On this page you can learn how to do a screen dump.
The program will dump the complete screen to a bmp-file.
First we must get a Device Context for the entire screen, this is done by getting a DC with HWND set to 0:
HDC dc = GetDC(0);If you want to dump a specific window to the file, you simply use the window's HWND in the call to GetDC.
Then we must get the size of the screen, here GetDeviceCaps comes handy:
int Width = GetDeviceCaps(dc, HORZRES); int Height = GetDeviceCaps(dc, VERTRES);Next we create a memory DC:
HDC MemDc = CreateCompatibleDC(dc);Then we create a bitmap, comatible with, and the size off, the screen DC, and tells windows to use this bitmap with the memory DC:
HBITMAP BitMap = CreateCompatibleBitmap(dc, Width, Height); SelectObject(MemDc, BitMap);Then we copy the image from the screen DC to the memory DC, and thus to our bitmap,
BitBlt(MemDc, 0, 0, Width, Height, dc, 0, 0, SRCCOPY);Then we are ready to start writing:
std::ofstream File("screen.bmp", std::ios::binary);Then we must know how many bits is used for each pixel on the screen:
BITMAP Bm; GetObject(BitMap, sizeof(Bm), &Bm);A bmp files contains tree parts, two headers and the data. The two headers can be written with WriteHeader which I created for the tutorial. To write the header is simply:
BITMAPINFOHEADER Header = WriteHeader(File, Width, Height, Bm.bmBitsPixel);Then we get the data from the bitmap and write them to the file:
char *Buffer = new char [Width*Height*Bm.bmBitsPixel/8]; GetDIBits(MemDc, BitMap, 0, Height, Buffer, (BITMAPINFO *)&Header, DIB_RGB_COLORS); File.write(Buffer, Height*Width*Bm.bmBitsPixel/8);And with a bit cleanup we are done:
ReleaseDC(0, dc); ReleaseDC(0, MemDc); delete [] Buffer; DeleteObject(BitMap);