alex2k8
alex2k8

Reputation: 43214

Black pictures when making screenshots with PrintWindow

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/

Upvotes: 6

Views: 5604

Answers (5)

Grey
Grey

Reputation: 23

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

Upvotes: 0

tigrou
tigrou

Reputation: 4516

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.

Upvotes: 0

Mysoft
Mysoft

Reputation: 1

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)

Upvotes: 0

hazzey
hazzey

Reputation: 1258

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.

Upvotes: 0

Error 454
Error 454

Reputation: 7315

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

Upvotes: 3

Related Questions