public partial class Form1 : Form
{
//导入 GetWindowDC函数 用于得到要截图窗口的句柄
[System.Runtime.InteropServices.DllImport("user32.dll")]
public extern static IntPtr GetWindowDC(IntPtr hwnd);
// 导入BitBlt函数 用于保存图片到目的地
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public extern static UInt64 BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight,
IntPtr hSrcDC, int xSrc, int ySrc, Int32 dwRop);
//...
//截图代码
Image Img = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(Img);
IntPtr ImageDC = g.GetHdc();
IntPtr SourceDC = GetWindowDC(this.Handle);
BitBlt(ImageDC, 0, 0, this.Width, this.Height, SourceDC, 0, 0, 13369376);
g.ReleaseHdc(ImageDC);
Img.Save("object.jpg");
<pre name="code" class="csharp"> }
c# 截图
最新推荐文章于 2024-10-28 09:43:40 发布