I can't seem to figure out how to do this, I have searched on google and found two code examples, one from a github screen capture library and another on a posting group and neither of them seem to be working.
I have a struct:
struct ClacksScreen
{
HWND hDesktopWnd;
int width, height;
RECT wr, cr;
HDC hdcClacksScreen; // hardware ClacksScreen
HDC hdcMemDC; // ClacksScreen in memory
HBITMAP hbmClacksScreen; //hbitmap of the ClacksScreen
BITMAP bmpClacksScreen;
BITMAPINFOHEADER bi;
};
This is updated. I have some functions defined, including one that writes the bitmap to disk, this works fine, the screen is captured and a bmp is written to disk and it's the one I wanted.
Now I want to convert the HBITMAP I take of the screen directly into a cv::Mat for OpenCV2.1.
It kind of works, except the image is pure grey and it crashes. Obviously I am still pretty n00b when it comes to c++ so there is probably something simple that I am just not groking.
static cv::Mat copyToCVMat(const ClacksScreen * s)
{
cv::Mat image;
image.create(s->bmpClacksScreen.bmWidth, s->bmpClacksScreen.bmHeight, CV_8UC4);
GetDIBits(s->hdcMemDC, s->hbmClacksScreen, 0,
(UINT)s->bmpClacksScreen.bmHeight,
image.data,
(BITMAPINFO *)&s->bi, DIB_RGB_COLORS);
return image;
}
When I wrap a cv::imwrite(image); in a try catch, I get a bad allocation error. Obviously at this point we've established that I have no frelling clue how to do this, so any help would be appreciated.
UPDATE
If I run this code:
try {
cv::Mat screen = cv::imread("captureqwsx.jpg");
if (!screen.data) {
printf("no image data?");
}
cv::imwrite("out.jpg",screen);
} catch(std::exception e) {
printf("Exception %s\n",e.what());
}
I get the output:
no image data? Exception bad allocation
When I try to run the high gui, it's the same as before, problem crops up for both .jpg and .bmps written to disk, which are viewable in image viewer and MS Paint fine.
I tried with a completely different image, a .png from a website, same issue.
So what am I doing wrong at this point?