views:

51

answers:

1

Hello people. I am using GDI+ to take window snapshot, the code is:

CComBSTR bstrfname (fname); 

HDC hdc = CreateCompatibleDC (hDC); 
HBITMAP hbmp = CreateCompatibleBitmap (hDC, CFG_WIDTH, CFG_HEIGHT); 
HBITMAP hbmp0 = (HBITMAP)SelectObject (hdc, hbmp); 
BitBlt (hdc, 0, 0, CFG_WIDTH, CFG_HEIGHT, hDC, 0, 0, SRCCOPY); 

Gdiplus::Bitmap *bmp = new Gdiplus::Bitmap (hbmp, NULL); 
CLSID encoderClsid; GetEncoderClsid (L"image/png", &encoderClsid); 
bmp->Save (bstrfname, &encoderClsid, NULL); 
delete bmp; 

SelectObject (hdc, hbmp0); 
DeleteObject (hbmp); 
DeleteDC (hdc); 

where hDC is set before with:

hWnd=CreateWindowEx(...); hDC=GetDC(hWnd); 

this works perfectly for small windows, but once I try windows bigger than screen, result looks like this:

http://yfrog.com/5n70452691p

i.e. taskbar is getting saved too. what gives?

A: 

This is normal, a screen-shot like this gives you exactly what you are looking at on your monitor. Including the taskbar. You will need to restrict the area you capture to the bounds of the window you want to capture. Use GetWindowRect() and adjust the size of the bitmap and the arguments you pass to BitBlt() accordingly.

PrintWindow can only work if the target window implements the WM_PRINT and WM_PRINTCLIENT message. Easy to implement but often overlooked.

Hans Passant
Thanks, but I am actually making this large window myself, so it's not like I am getting out of its rect accidentally. From what I read at the link above, it looks what I want might be not possible at all.
fuck
Perhaps I should have asked another question instead, how do I capture window including obscured/offscreen areas, and the answer to that is, most probably, no way.
fuck
Well, that's why PrintWindow is there. But if the window doesn't support it, or contains child windows that don't support it, then you're done.
Hans Passant