how can i make screen-shots of the local pc screen with win32 c++?
A:
- Use
GetDC(NULL);
to get a DC for the entire screen. - Use
CreateCompatibleDC
to get a compatible DC. - Use
CreateCompatibleBitmap
to create a bitmap to hold the result. - Use
SelectObject
to select the bitmap into the compatible DC. - Use
BitBlt
to copy from the screen DC to the compatible DC. - Deselect the bitmap from the compatible DC.
When you create the compatible bitmap, you want it compatible with the screen DC, not the compatible DC.
Jerry Coffin
2010-07-20 14:53:56
+2
A:
// get the device context of the screen
hScreenDC = CreateDC("DISPLAY", NULL, NULL, NULL);
// and a device context to put it in
hMemoryDC = CreateCompatibleDC(hScrDC);
x = GetDeviceCaps(hScreenDC, HORZRES);
y = GetDeviceCaps(hScreenDC, VERTRES);
// maybe worth checking these are positive values
hBitmap = CreateCompatibleBitmap(hScreenDc, x, y);
// get a new bitmap
hOldBitmap = SelectObject(hMemoryDC, hBitmap)
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
hBitmap = SelectObject(hMemoryDC, hOldBitmap);
// clean up
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
// now your image is held in hBitmap. You can save it or do whatever with it
Woody
2010-07-20 15:07:56
This works on all nt based windows from Windows NT4 to Windows 7.
Woody
2010-07-20 15:11:10
@Woody: Why are you using CreateDC and not just GetDC(NULL)?
Anders
2010-07-21 03:57:43
Honestly I haven't looked at it for a while, this is code from quite a way back which I have been using in an application. It works in everything so I have never gone back to it!If GetDC would be better, I can ammend the answer.
Woody
2010-07-21 08:04:15