用C#,VS,请问如何截屏winform的界面呢?(不是截全屏幕)
截自己的还是其他窗口?
可以用
Graphics.CopyFromScreen
从屏幕中截取一块
也可以用
Control.DrawToBitmap
将控件(窗体 Form 也是控件)图像画到位图中
他们都是基于显卡的 ReadPixels 函数的
手段又很多,上面说的都可以用。
我这里多写一个调用win32api的(利用的是print window)----注意需要利用窗体句柄,所以你得先确定窗体句柄已经生成
private async void button1_Click(object sender, EventArgs e)
{
//
var bitmap = GetWindow(this.Handle);
bitmap.Save(@"d:\tt01.jpg", ImageFormat.Jpeg);
}
[DllImport("gdi32.dll")]
static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
private static Bitmap GetWindow(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
Control control = Control.FromHandle(hWnd);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}