Originally Posted by
bithub
I'm confused. Isn't getting a screenshot as easy as
GetDC(NULL) followed by
BitBlt()? I may be missing something here, but I think the only part of this that requires any effort is JPEG encoding the pixels.
Thanks for the reply. But after a few hours of googling (because not only did I want to take a screenshot, but save it too), and messing with a bit of code, I managed to put this all together...problem is...it doesn't work, and I can't seem to guess why. I've attempted to debug it but all I get is just a 3MB bitmap file thats floodfilled with black. Heres the code.
Code:
HDC Screenshot = GetDC(NULL);
int ResX = GetSystemMetrics(SM_CXSCREEN);
int ResY = GetSystemMetrics(SM_CYSCREEN);
HDC MemDesktop = CreateCompatibleDC(Screenshot);
HBITMAP tmpbitmap = CreateCompatibleBitmap(MemDesktop, ResX, ResY);
HBITMAP bitmap = (HBITMAP)SelectObject(MemDesktop, tmpbitmap);
BitBlt(MemDesktop, 0,0, ResX, ResY, Screenshot, 0,0, SRCCOPY);
BITMAPINFO BitInfo;
memset(&BitInfo, 0, sizeof(BITMAPINFO));
BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitInfo.bmiHeader.biWidth = ResX;
BitInfo.bmiHeader.biHeight = ResY;
BitInfo.bmiHeader.biPlanes = 1;
BitInfo.bmiHeader.biBitCount = 16;
BitInfo.bmiHeader.biCompression = BI_RGB; //BI_JPEG;
BitInfo.bmiHeader.biSizeImage = (BitInfo.bmiHeader.biWidth * BitInfo.bmiHeader.biHeight) * 2;
void *bits = malloc(BitInfo.bmiHeader.biSizeImage);
GetDIBits(MemDesktop, bitmap, 0, BitInfo.bmiHeader.biHeight, bits, &BitInfo, DIB_PAL_COLORS);
HANDLE bmfile = CreateFile("image.bmp", GENERIC_READ | GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
BITMAPFILEHEADER bitheader;
bitheader.bfReserved1 = 0;
bitheader.bfReserved2 = 0;
bitheader.bfType = 0x4d42;
bitheader.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
bitheader.bfSize = bitheader.bfOffBits + BitInfo.bmiHeader.biSizeImage;
DWORD bytes;
WriteFile(bmfile, &bitheader, sizeof(BITMAPFILEHEADER), &bytes, NULL);
WriteFile(bmfile, &BitInfo.bmiHeader, sizeof(BITMAPINFOHEADER), &bytes, NULL);
WriteFile(bmfile, bits, BitInfo.bmiHeader.biSizeImage, &bytes, NULL);
CloseHandle(bmfile);
free(bits);
DeleteDC(MemDesktop);
DeleteDC(Screenshot);
DeleteObject(bitmap);
DeleteObject(tmpbitmap);