I am new to using GDI+ and I'm doing a proof of concept for a personal project of mine.
For this code segment, I essentially want to convert a cv::Mat
into a bitmap and save the file to an arbitrary path.
My conversion process is adapted from this StackOverflow answer:
How to convert an opencv mat image to gdi bitmap
My problem is that when I call bitmap.Save()
, it returns an error code of 2 (InvalidParameter).
I don't understand why one or more of my input parameters is wrong.
cv::Mat initialization
cv::Mat mat = cv::Mat::ones(768, 1366, CV_8UC3);
show_image_from_mat(mat);
The show_image_from_mat function
int show_image_from_mat(cv::Mat image)
{
cv::Size size = image.size();
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Initialize GDI+.
printf("================================================================");
printf("\n%i StarupStatus ",GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL));
// gdiplus
Gdiplus::Bitmap bitmap(size.width, size.height, image.step1(), PixelFormat24bppRGB, image.data);
CLSID bmpClsid;
printf("\n%p result", CLSIDFromString(L"{557cf400-1a04-11d3-9a73-0000f81ef32e}",&bmpClsid));
printf("\n%p clsid", bmpClsid);
const WCHAR* filename = L"C:\\output.bmp";
char shortfilename[15];//You will need to malloc if you need to handle arbitrary filenames.
printf("\n%i ErrorCode",bitmap.Save(filename, &bmpClsid,NULL));
wcstombs(shortfilename, filename, 15);
show_image_from_file(shortfilename);
GdiplusShutdown(gdiplusToken);
return 9001;
}
I got the CLSID abomination from Does GDI+ have standard image encoder CLSIDs?.
And I am writing this in Visual Studio (I'm also new to this).
If the image doesn't load, my output is this: