Black pictures when making screenshots with PrintWindow
Asked Answered
E

5

6

I am doing the screen shots of IE using PrintWindow. The problem is that some times I get images with black areas. It may be a whole html content what is black, some times only certain areas are black.

The content of the IE is NOT changed between taking shots.

What is strange is that on some computers I get black images very oftern, on some I never get them.

I tested with Fx, and had same black images.

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;
}

I have found some links, but they give no answer:

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/

Earpiercing answered 14/8, 2009 at 19:56 Comment(4)
Is there a correlation between black areas and SWF/Flash includes on the page?Harlen
No. I got black holes with simple www.google.com.Earpiercing
I see the problem on Windows 2008. While XP, Vista and Windows 7 seems to be fine.Earpiercing
There must be a way to do this properly because windows is able to show a thumbnail of such applications in the task bar properly (eg: it's not a black image). Same if you press "Window + Tab" keys.Anon
A
3

This seems to be common when taking screenshots of applications that are using the GPU. BitBlt can successfully copy pixels where PrintWindow fails.

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);
Adduction answered 27/11, 2011 at 11:48 Comment(0)
P
0

The issue is that not all programs provide the needed functions to redraw the window when given the PrintWindow function or the WM_PRINT message.

Prostatitis answered 22/6, 2014 at 15:1 Comment(0)
E
0

use SetWindowLong to set WS_EX_COMPOSITED do the PrintWindow() set it back to what was before (or leave it with COMPOSITED to speed up... but that will affect the visibility of the real window unless hw acc is disabled) maybe trying to see if WS_EX_LAYERED and setting opacity to 254 would work better

(forgot to say... that this works, but only for the top level window, trying to PrintWindow some child wont work, even if you set the composited on the top level window)

Evaporimeter answered 10/3, 2019 at 1:2 Comment(0)
A
0

You might take a look at Windows.Graphics.Capture. This a fairly new API that requires Windows 10 version 1803 or better. There is a some code example here.

It should work with applications that use GPU acceleration, such as Chrome. This is what OBS use being the scenes when you chose "Windows 10" capture method.

Anon answered 18/11, 2021 at 20:56 Comment(0)
M
0

Set the nFlags to PW_RENDERFULLCONTENT rather than 0, this requires windows 8.1 and later os version, have a try.

Mars answered 12/3 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.