Hi, how would I save a HDC to a bitmap? ..its to take screenshots
thx
(c++ win32) How to save a DC to a bitmap?
Started by johnnyBravo, May 26 2006 05:30 AM
6 replies to this topic
Ad:
#2 Anonymous Poster_Anonymous Poster_* Guests - Reputation:
Posted 26 May 2006 - 05:34 AM
Dude, did you even try to google first?
#4 Members - Reputation: 518
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.
[Edited by - yadango on May 27, 2006 12:16:53 AM]
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:
Posted 26 May 2006 - 05:54 PM
that code's got one huge memory leak not to mention the GDI leaks
#7 Members - Reputation: 100
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?
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?


















