5

我正在使用 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/

4

4 回答 4

2

在截取使用 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);
于 2011-11-27T11:48:54.667 回答
0

问题是,当给定PrintWindow 函数WM_PRINT 消息时,并非所有程序都提供重绘窗口所需的函数。

于 2014-06-22T15:01:16.810 回答
0

使用 SetWindowLong 设置 WS_EX_COMPOSITED 执行 PrintWindow() 将其设置回以前的状态(或将其保留为 COMPOSITED 以加快速度......但这会影响真实窗口的可见性,除非禁用 hw acc)也许试图看看是否WS_EX_LAYERED 并将不透明度设置为 254 会更好

(忘了说...这有效,但仅适用于顶级窗口,即使您在顶级窗口上设置了合成,尝试 PrintWindow 一些孩子也不会工作)

于 2019-03-10T01:02:12.870 回答
0

你可以看看Windows.Graphics.Capture。这是一个相当新的 API,需要 Windows 10 版本 1803 或更高版本。这里有一些代码示例。

它应该适用于使用 GPU 加速的应用程序,例如 Chrome。当您选择“Windows 10”捕获方法时,这就是 OBS 使用的场景。

于 2021-11-18T20:56:56.557 回答