Jump to content



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

  • You cannot reply to this topic
6 replies to this topic

#1 johnnyBravo   Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 May 2006 - 05:30 AM

Hi, how would I save a HDC to a bitmap? ..its to take screenshots thx

Ad:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 26 May 2006 - 05:34 AM

Dude, did you even try to google first?

#3 kuphryn   Members   -  Reputation: 210

Like
0Likes
Like

Posted 26 May 2006 - 12:19 PM

CreateDIBSection()
StretchBlt()

Kuphryn

#4 yadango   Members   -  Reputation: 518

Like
0Likes
Like

Posted 26 May 2006 - 05:16 PM

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]

#5 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 26 May 2006 - 05:54 PM

that code's got one huge memory leak not to mention the GDI leaks

#6 yadango   Members   -  Reputation: 518

Like
0Likes
Like

Posted 26 May 2006 - 06:03 PM

good eye. forgot to add DeleteObject ha ha ha.

#7 johnnyBravo   Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 May 2006 - 09:59 PM

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?







We are working on generating results for this topic
PARTNERS