使用PrintWindow制作屏幕截图时的黑色图片

ale*_*2k8 5 winapi screenshot

我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)

我找到了一些链接,但他们没有回答:

http://www.vbforums.com/showthread.php?t=555250 http://www.codeguru.com/forum/archive/index.php/t-357211.html http://social.msdn.microsoft. COM /论坛/ EN-US /的WinForms /线程/ 3e3decd8-ced1-4f17-a745-466e5aa91391 /

Err*_*454 3

在对使用 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);
Run Code Online (Sandbox Code Playgroud)