Hi, I have this function for taking a screenshot and extracting the pixels, it is working but I have a feeling it could be optimized and I don't really know what all the commands do. Can you spot any superflous steps or any other issues?
Code:
bool GetScreenshot()
{
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
HDC hdcScreen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
HDC hdcShot = CreateCompatibleDC(hdcScreen);
HBITMAP hbmap = CreateCompatibleBitmap(hdcScreen, screenWidth, screenHeight);
HBITMAP hBitmapOld = (HBITMAP) SelectObject(hdcShot, hbmap);
BitBlt(hdcShot, 0, 0, screenWidth, screenHeight, hdcScreen, 0, 0, SRCCOPY);
BITMAP bm;
if (!GetObject(hbmap, sizeof(BITMAP), (LPSTR)&bm)) return false;
int bitsPerPixel = bm.bmBitsPixel;
int bwidth = bm.bmWidth;
int bheight = bm.bmHeight;
if (bitsPerPixel != 32 || bm.bmPlanes != 1) return false;
BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = bwidth;
bmi.bmiHeader.biHeight = bheight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = bitsPerPixel;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
BYTE* bits = new BYTE[bwidth * bheight * 4];
if (!bits) return false;
HDC hdc = GetDC(GetDesktopWindow());
if (!GetDIBits(hdc, hbmap, 0, bheight, bits, &bmi, DIB_RGB_COLORS))
{
ReleaseDC(GetDesktopWindow(), hdc);
delete [] bits;
return false;
}
ReleaseDC(GetDesktopWindow(), hdc);
//Do things with bits here
SelectObject(hdcShot, hBitmapOld);
DeleteObject(hbmap);
DeleteDC(hdcShot);
DeleteDC(hdcScreen);
return true;
}
Thanks!