Skip to main content
GameDev.net gamedev.net
🔒 Locked

(c++ win32) How to save a DC to a bitmap?

Started by johnnyBravo May 26, 2006 at 11:30 AM 5 replies 25.1k views
Original Post
johnnyBravo
johnnyBravo
Hi, how would I save a HDC to a bitmap? ..its to take screenshots thx
kuphryn
kuphryn
CreateDIBSection()
StretchBlt()

Kuphryn
yadango
yadango
Here, you can have my code. Though there may have to be some
changes done to it as it is like written way back for Win32s.
For Direct3D however, you might need to use the surface data,
because of hardware acceleration, GDI caps more than likely
won't work. There is also another way to do this by sending
the WM_PRINT message.

void CaptureScreen(HWND window, const char* filename){ // get screen rectangle RECT windowRect; GetWindowRect(window, &windowRect); // bitmap dimensions int bitmap_dx = windowRect.right - windowRect.left; int bitmap_dy = windowRect.bottom - windowRect.top; // create file ofstream file(filename, ios::binary); if(!file) return; // save bitmap file headers BITMAPFILEHEADER fileHeader; BITMAPINFOHEADER infoHeader; fileHeader.bfType      = 0x4d42; fileHeader.bfSize      = 0; fileHeader.bfReserved1 = 0; fileHeader.bfReserved2 = 0; fileHeader.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); infoHeader.biSize          = sizeof(infoHeader); infoHeader.biWidth         = bitmap_dx; infoHeader.biHeight        = bitmap_dy; infoHeader.biPlanes        = 1; infoHeader.biBitCount      = 24; infoHeader.biCompression   = BI_RGB; infoHeader.biSizeImage     = 0; infoHeader.biXPelsPerMeter = 0; infoHeader.biYPelsPerMeter = 0; infoHeader.biClrUsed       = 0; infoHeader.biClrImportant  = 0; file.write((char*)&fileHeader, sizeof(fileHeader)); file.write((char*)&infoHeader, sizeof(infoHeader)); // dibsection information BITMAPINFO info; info.bmiHeader = infoHeader;  // ------------------ // THE IMPORTANT CODE // ------------------ // create a dibsection and blit the window contents to the bitmap HDC winDC = GetWindowDC(window); HDC memDC = CreateCompatibleDC(winDC); BYTE* memory = 0; HBITMAP bitmap = CreateDIBSection(winDC, &info, DIB_RGB_COLORS, (void**)&memory, 0, 0); SelectObject(memDC, bitmap); BitBlt(memDC, 0, 0, bitmap_dx, bitmap_dy, winDC, 0, 0, SRCCOPY); DeleteDC(memDC); ReleaseDC(window, winDC); // save dibsection data int bytes = (((24*bitmap_dx + 31) & (~31))/8)*bitmap_dy; file.write(memory, bytes); // HA HA, forgot paste in the DeleteObject lol, happy now ;)? DeleteObject(bitmap);}


[Edited by - yadango on May 27, 2006 12:16:53 AM]
yadango
yadango
good eye. forgot to add DeleteObject ha ha ha.
johnnyBravo
johnnyBravo
if I use this instead:
HDC winDC = CreateDC("DISPLAY", 0,0,0);

do i just delete it: DeleteDC(winDC); ?

Also is there a way to capture the pixels from videos playing aswell. I know that they are playing on a different layer, but isn't there some higher level function of getting the pixels onscreen?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.