i tried to run this code on a game and it doesn t work(it gets a black screen as ouput)
the only app that worked partially was the File Explorer -Home Window or Gallery Window,but the image is not positioned well in the output,and it doesn t show the entire window(the output is partially black)
This is the code i’ve been using:
cv::Mat getMat(HWND wnd) {
HDC deviceContext = GetDC(wnd);
HDC memoryDeviceContext = CreateCompatibleDC(deviceContext);
RECT windowRect;
GetClientRect(wnd, &windowRect);
int height = windowRect.bottom;
int width = windowRect.right;
HBITMAP bitmap = CreateCompatibleBitmap(deviceContext, width, height);
SelectObject(memoryDeviceContext, bitmap);
//copy
BitBlt(memoryDeviceContext, 0, 0, width, height, deviceContext, 0, 0, SRCCOPY);
//specify format by using bitmapinfoheader
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = -height;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 1;
cv::Mat mat = cv::Mat(height, width, CV_8UC4); //8 biti int+4canale;
//transform datele si le pun in mat
GetDIBits(memoryDeviceContext, bitmap, 0, height, mat.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
//curat;
DeleteObject(bitmap);
DeleteDC(memoryDeviceContext);
ReleaseDC(wnd, deviceContext);
return mat;
}
New contributor
Andrei Burlacu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.