我正在使用Native GDI +捕获窗口"屏幕上".屏幕关闭时它不起作用(生成黑色图像).我该如何解决?(我正在使用.Net 4.5)
public static Image CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = User32.GetWindowDC(handle);
RECT windowRect = new RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);
Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
Gdi32.SelectObject(hdcDest, hOld);
Gdi32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
Image image = Image.FromHbitmap(hBitmap);
Gdi32.DeleteObject(hBitmap);
return image;
}
Run Code Online (Sandbox Code Playgroud)
小智 0
据我所知,当没有人接收图像时,图形驱动程序会停止生成图像,以节省电量。这可能就是为什么当屏幕“关闭”时无法捕获图像的原因。当没有人接收图像时,使用 CPU、GPU 和总线电源来生成图像是没有意义的。
| 归档时间: |
|
| 查看次数: |
168 次 |
| 最近记录: |