user63898 user63898 - 1 year ago 97
C++ Question

How can I take a screenshot in a windows application?

How can I take a screenshot of the current screen using Win32?

Answer Source
// get the device context of the screen
HDC hScreenDC = CreateDC("DISPLAY", NULL, NULL, NULL);     
// and a device context to put it in
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

int width = GetDeviceCaps(hScreenDC, HORZRES);
int height = GetDeviceCaps(hScreenDC, VERTRES);

// maybe worth checking these are positive values
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);

// get a new bitmap
HBITMAP hOldBitmap = SelectObject(hMemoryDC, hBitmap);

BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
hBitmap = SelectObject(hMemoryDC, hOldBitmap);

// clean up

// now your image is held in hBitmap. You can save it or do whatever with it
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download