The screenshot should be able to be in any position & have any width/height.
I want to save just x,y - width,height from the screen_dc and store that data in mem_dc starting at 0,0 - width,height.
My current code:
std::vector<RGBQUAD> recvScreenSnippet(int x, int y, int width, int height) {
std::vector<RGBQUAD> v_screen_data;
HDC const screen_dc = GetDC(NULL);
HDC const mem_dc = CreateCompatibleDC(NULL);
HBITMAP const hBmp = CreateCompatibleBitmap(screen_dc, width, height);
auto const oldBmp = SelectObject(mem_dc, hBmp);
// 32 bit & Upside Down (Y-Flipped)
BITMAPINFO bmi{};
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
// Receive pixel data from hdc
BitBlt(mem_dc, 0, 0, width, height, screen_dc, x, y, SRCCOPY);
GetDIBits(mem_dc, hBmp, 0, height, &v_screen_data[0], &bmi, DIB_RGB_COLORS);
// Cleanup
SelectObject(mem_dc, oldBmp);
DeleteObject(hBmp);
DeleteDC(mem_dc);
ReleaseDC(0, screen_dc);
return v_screen_data;
}
This should go 10 pixels down & right and then create a screenshot from that position with a width & height of 30 pixels.
std::vector<RGBQUAD> v_screen_snippet = recvScreenSnippet(10, 10, 30, 30);
My question is: Am I doing it right, or do I understand the BitBlt wrong, since I have seen many examples, where the offset has been inserted in the first parameters, instead of the last ones.
BitBlt(mem_dc, CONFUSING, CONFUSING, width, height, screen_dc, CONFUSING, CONFUSING, SRCCOPY);
I have read the documentation, but I am still very confused, I would appreciate help.
I tried changing the parameters around, but still very confused.
The first X/Y pair (2nd, 3rd parameters) is the location in the destination bitmap where the data will be copied to.
The second X/Y pair (7th, 8th parameters) is the location in the source bitmap where the data will be copied from.
For a typical screen shot, the first pair will always be 0,0, and the second will be the top, left corner of the rectangle on the screen the user wants in the screen-shot (which seems to fit with the code you have).