typedef union RGBA
{
std::uint32_t Colour;
struct
{
std::uint8_t B, G, R, A;
};
} *PRGB;
std::vector<RGBA> GetPixels(HWND Window, RECT Area, int BitsPerPixel) {
int width = Area.right - Area.left;
int height = Area.bottom - Area.top;
HDC DC = GetDC(Window);
HDC SDC = CreateCompatibleDC(DC);
HBITMAP hSBmp = CreateCompatibleBitmap(DC, width, height);
DeleteObject(SelectObject(SDC, hSBmp));
BitBlt(SDC, 0, 0, width, height, DC, Area.left, Area.top, SRCCOPY);
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = height;
bi.biPlanes = 1;
bi.biBitCount = BitsPerPixel;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
std::size_t size = ((width * bi.biBitCount + 31) / 32) * 4 * height;
std::vector<std::uint8_t>Data(size);
GetDIBits(SDC, hSBmp, 0, height, Data.data(), (BITMAPINFO*)&bi, DIB_RGB_COLORS);
DeleteDC(SDC);
DeleteObject(hSBmp);
ReleaseDC(Window, DC);
std::vector<RGBA> Pixels;
Pixels.resize(width * height);
std::uint8_t* Buffer = Data.data();
if (BitsPerPixel == 24)
Buffer += (std::abs((int)width) * 3) & 3;
for (size_t I = 0; I < height; ++I) {
memcpy(Pixels[(height - 1 - I) * width], Buffer, 4 * width);
Buffer += 4 * width;
}
return Pixels;
}