I'm trying to take an 100 by 100 square screenshot of the upper left of my screen and then print all the pixel color hexadecimal values to the console. Right now it is just printing blank spaces.
Here's my code:
#include <wingdi.h>
#include <windows.h>
#include <iostream>
using namespace std;
int main(){
HWND desktop = GetDesktopWindow();
HDC desktopHdc = GetDC(desktop);
HDC hdc = CreateCompatibleDC(desktopHdc);
HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hdc, 100, 100);
SelectObject(hdc, hCaptureBitmap);
BITMAPINFO bmi = {0};
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = 100;
bmi.bmiHeader.biHeight = 100;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
RGBQUAD *pPixels = new RGBQUAD[100 * 100];
BitBlt(hdc, 0, 0, 100, 100, desktopHdc, 0, 0, SRCCOPY);
GetDIBits(hdc, hCaptureBitmap, 0, 100, pPixels, &bmi, DIB_RGB_COLORS);
for (int i = 0; i < 10000; i++){
cout << static_cast<unsigned int>(pPixels[i].rgbRed) << static_cast<unsigned int>(pPixels[i].rgbGreen) << static_cast<unsigned int>(pPixels[i].rgbBlue) << "\n";
}
}
P.S. I'm pretty sure I'm supposed to use 'BitBLT' to get a square of pixels, but was confused on how to use that function, so any tips on that would be great too.
Thanks!
Here's a helpful function:
void Print_RGBQUAD(const RGBQUAD& r)
{
std::cout << " " << setw(2) << hex << (unsigned int)(r.rgbRed);
std::cout << " " << setw(2) << hex << (unsigned int)(r.rgbGreen);
std::cout << " " << setw(2) << hex << (unsigned int)(r.rgbBlue);
std::cout << " ";
}
This example shows one fundamental method for printing an RGBQUAD structure.
The order is changed to RGB. The structure, defined by Microsoft Docs, is:
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;