I don't know much about the GDI, but I heard it can take screenshots so I attempted to find a bit of code to do so. Problem is, after alot of debugging, I still have no idea why it isn't working.
Whats suppose to happen: Screenshot of the desktop.
What does happen: fully blank image filled with black.
Its probably just a simple error that I overlooked, but I can't seem to find it.
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_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);