为什么我的 Gdiplus::Bitmap 不保存文件

Why won't my Gdiplus::Bitmap save a file

我刚开始使用 GDI+,我正在为我的个人项目做概念验证。

对于此代码段,我主要是想将 cv::Mat 转换为位图并将文件保存到任意路径。

我的转换过程改编自这个 Whosebug 答案:

How to convert an opencv mat image to gdi bitmap

我的问题是,当我调用 bitmap.Save() 时,它 returns 错误代码为 2 (InvalidParameter)。

我不明白为什么我的一个或多个输入参数是错误的。

cv::Mat 初始化

cv::Mat mat = cv::Mat::ones(768, 1366, CV_8UC3);
show_image_from_mat(mat);

show_image_from_mat函数

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;
}

我从 Does GDI+ have standard image encoder CLSIDs?.

那里得到了可憎的 CLSID

我正在 Visual Studio 中写这篇文章(我也是新手)。

如果图片没有加载,我的输出是这样的:

答案是您所有答案的组合。我的一个八位字节在我的 CLSID 中是错误的,输出路径不在当前用户的范围内。我的 Bitmap Constructor 的输入参数要求 Stride 参数(我将 image.step1() 推入的参数)可以被 4 整除。我收到的状态错误代码 2 是从我的 Bitmap Constructor