110,532
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 得到窗口截图
/// </summary>
/// <param name="hWnd"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Bitmap GetImageFromWindow(IntPtr hWnd, int width, int height)
{
IntPtr hscrdc = user32DLL.GetWindowDC(hWnd);
IntPtr hbitmap = gdi32DLL.CreateCompatibleBitmap(hscrdc, width, height);
IntPtr hmemdc = gdi32DLL.CreateCompatibleDC(hscrdc);
gdi32DLL.SelectObject(hmemdc, hbitmap);
user32DLL.PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
gdi32DLL.DeleteDC(hscrdc);//删除用过的对象
gdi32DLL.DeleteObject(hbitmap);//删除用过的对象
gdi32DLL.DeleteDC(hmemdc);//删除用过的对象
return bmp;
}
public Bitmap GetWindowCapture(int hWnd)
{
IntPtr intp; Bitmap bmp = null;
if (hWnd == 0) { intp = Win32API.GetDesktopWindow(); } else { intp = (IntPtr)hWnd; }
IntPtr hscrdc = Win32API.GetWindowDC(intp);IntPtr hmemdc = Win32API.CreateCompatibleDC(hscrdc);
try
{
Win32API.RECT rect = new Win32API.RECT();
Win32API.GetClientRect(intp, ref rect);
int width = rect.right - rect.left; int height = rect.bottom - rect.top;
MessageBox.Show(width.ToString() + "/" + height.ToString());
this.pictureBox1.Width = width; this.pictureBox1.Height = height;
IntPtr hbitmap = Win32API.CreateCompatibleBitmap(hscrdc, width, height);
Win32API.SelectObject(hmemdc, hbitmap);
Win32API.PrintWindow(intp, hmemdc, 0);
bmp = Bitmap.FromHbitmap(hbitmap);
//Clipboard.SetImage(bmp);
}
catch { }
finally { Win32API.DeleteDC(hmemdc); Win32API.DeleteDC(hscrdc); }
return bmp;
}
这个可以,不过我也发现问题了,这个窗体尺寸貌似捕获的大小不对,偏小了一点,不知道为什么,请大侠指点下下这是为什么呢/// <summary>
/// 得到窗口截图
/// </summary>
/// <param name="hWnd"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public Bitmap GetImageFromWindow(IntPtr hWnd, int width, int height)
{
Bitmap bitmap = null;
IntPtr hscrdc = user32DLL.GetWindowDC(hWnd);
IntPtr hbitmap = gdi32DLL.CreateCompatibleBitmap(hscrdc, width, height);
IntPtr hmemdc = gdi32DLL.CreateCompatibleDC(hscrdc);
try
{
DateTime dtStart = DateTime.Now;
gdi32DLL.SelectObject(hmemdc, hbitmap);
user32DLL.PrintWindow(hWnd, hmemdc, 0);
bitmap = Bitmap.FromHbitmap(hbitmap);
bool not = notAllBlack(bitmap, 500);
int count = 0;
while (!not)
{
user32DLL.PrintWindow(hWnd, hmemdc, 0);
bitmap = Bitmap.FromHbitmap(hbitmap);
not = notAllBlack(bitmap, 500);
if (!not)
{
bitmap.Dispose();
GC.Collect();
Application.DoEvents();
}
count++;
}
DateTime dtEnd = DateTime.Now;
TimeSpan jianju = dtEnd - dtStart;
richTextBox1.Text += "全部都是黑" + count + ",用时:" + jianju.TotalSeconds + "秒\r\n";
}
catch (Exception ex)
{
throw ex;
}
finally
{
//gdi32DLL.DeleteDC(hscrdc);//删除用过的对象
gdi32DLL.DeleteObject(hbitmap);//删除用过的对象
gdi32DLL.DeleteDC(hmemdc);//删除用过的对象
gdi32DLL.ReleaseDC(hWnd, hscrdc);
}
return bitmap;
}
public Bitmap GetBitmapFromDC(IntPtr hwnd, int width,int height)
{
IntPtr windc = user32DLL.GetDC(hwnd);//获取窗口DC
IntPtr hDCMem = gdi32DLL.CreateCompatibleDC(windc);//为设备描述表创建兼容的内存设备描述表
IntPtr hbitmap = gdi32DLL.CreateCompatibleBitmap(windc, width, height);
IntPtr hOldBitmap = (IntPtr)gdi32DLL.SelectObject(hDCMem, hbitmap);
gdi32DLL.BitBlt(hDCMem, 0, 0, width, height, windc, 0, 0, gdi32DLL.SRCCOPY);
hbitmap = (IntPtr)gdi32DLL.SelectObject(hDCMem, hOldBitmap);
Bitmap bitmap = Bitmap.FromHbitmap(hbitmap);
gdi32DLL.DeleteObject(hbitmap);//删除用过的对象
gdi32DLL.DeleteObject(hOldBitmap);//删除用过的对象
gdi32DLL.DeleteDC(hDCMem);//删除用过的对象
user32DLL.ReleaseDC(hwnd, windc);
return bitmap;
}