Stencil
Stencil

Reputation: 1873

Windows GDI Context - Displaying Bitmaps

I want to catch a Desktop frame and store it in a HBITMAP struct. Then, after creating a proper memory device context out of my application main window's device context, I would select the HBITMAP into it and the use StretchBlt to display the bitmap.

But this doesn't work as expected, because it just shows a black frame. Both hdc and mem_hdc are respectively the device context and memory device context of main window initialized before.

Here's the code:

...
hDC desk_hdc, desk_mem_hdc;
BITMAP bitmap;
HBITMAP hbitmap;
desk_hdc = GetDC(NULL);

hbitmap = CreateCompatibleBitmap(desk_hdc, GetDeviceCaps(desk_hdc, HORZRES), GetDeviceCaps(desk_hdc, VERTRES));
GetObject(hbitmap, sizeof(BITMAP), &bitmap);
SelectObject(mem_hdc, hbitmap);
StretchBlt(hdc, 0, 0, 1024, 768, mem_hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY|CAPTUREBLT|NOMIRRORBITMAP);
...

Upvotes: 1

Views: 608

Answers (1)

K-ballo
K-ballo

Reputation: 81349

The source dc of your StretchBlt operation is mem_hdc, which has a compatible uninitialized bitmap. That's why you get a black frame.

If you want to capture the desktop contents, you have to first copy it to the bitmap in your mem_hdc. Just after SelectObject do:

BitBlt( mem_hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, desk_hdc, 0, 0, SRCCOPY );

Upvotes: 2

Related Questions