我正在使用 PrintWindow 制作 IE 的屏幕截图。问题是有时我会得到带有黑色区域的图像。它可能是整个 html 内容什么是黑色的,有时只有某些区域是黑色的。
IE 的内容在拍摄之间不会改变。
奇怪的是,在某些计算机上我经常得到黑色图像,而在某些计算机上我从来没有得到它们。
我用 Fx 测试过,有相同的黑色图像。
HBITMAP ShootWindow(HWND hWnd)
{
RECT rect = {0};
GetWindowRect(hWnd, & rect);
HDC hDC = GetDC(hWnd);
if(hDC == NULL)
throw "GetDC failed.";
HDC hTargetDC = CreateCompatibleDC(hDC);
if(hTargetDC == NULL)
throw "CreateCompatibleDC failed.";
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rect.right - rect.left, rect.bottom - rect.top);
if(hBitmap == NULL)
throw "CreateCompatibleBitmap failed.";
if(!SelectObject(hTargetDC, hBitmap))
throw "SelectObject failed.";
if(!PrintWindow(hWnd, hTargetDC, 0))
throw "PrintWindow failed.";
ReleaseDC(hWnd, hDC);
ReleaseDC(hWnd, hTargetDC);
return hBitmap;
}
我找到了一些链接,但他们没有给出答案:
http://www.vbforums.com/showthread.php?t=555250 http://www.codeguru.com/forum/archive/index.php/t-357211.html http://social.msdn.microsoft.com/forums/en-US/winforms/thread/3e3decd8-ced1-4f17-a745-466e5aa91391/
最佳答案
在截取使用 GPU 的应用程序的屏幕截图时,这似乎很常见。 BitBlt 可以成功复制 PrintWindow 失败的像素。
WINDOWINFO wi;
GetWindowInfo(hWnd, &wi);
BitBlt(hdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, hDC, wi.rcClient.left, wi.rcClient.top, SRCCOPY);
关于winapi - 使用 PrintWindow 制作屏幕截图时出现黑色图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1279867/