【问题标题】:Cannot delete GDI+ Image无法删除 GDI+ 图像
【发布时间】:2014-05-17 12:23:13
【问题描述】:

我正在编写一个基本的图像转换器来将图像转换为 BMP。我最后清除了图像以避免内存泄漏。但是,当我尝试编译它时,出现了这个错误:

类型 'class Gdiplus::Image' 参数赋予'delete',预期指针

我检查了多个网站,但是当我使用他们的示例时,它仍然会出现编译器错误。甚至微软的例子也出现了这个错误!我看到一个网站包含删除图片的方法,但我不记得链接或他们删除图片的方式。

我的代码:

#include <windows.h>
#include <gdiplus.h>

using namespace Gdiplus;

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
    using namespace Gdiplus;    UINT  num = 0;          // number of image encoders
    UINT  size = 0;         // size of the image encoder array in bytes

    ImageCodecInfo* pImageCodecInfo = NULL;

    GetImageEncodersSize(&num, &size);
    if(size == 0)
        return -1;  // Failure

    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
    if(pImageCodecInfo == NULL)
    return -1;  // Failure

    GetImageEncoders(num, size, pImageCodecInfo);

    for(UINT j = 0; j < num; ++j)
    {
        if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
        {
            *pClsid = pImageCodecInfo[j].Clsid;
            free(pImageCodecInfo);
            return j;  // Success
        }
    }

    free(pImageCodecInfo);
    return 0;
}


int main()
{
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    CLSID bmpClsid;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    Image picture(L"TEST.GIF");
    GetEncoderClsid(L"image/bmp", &bmpClsid);
    picture.Save(L"Mosaic2.bmp", &bmpClsid, NULL);
    delete picture;
    GdiplusShutdown(gdiplusToken);
    return 0;
}

如果您给我一个有效的答案,我会将您记入该计划的学分中。 谢谢!

【问题讨论】:

    标签: c++ winapi bitmap gdi+ delete-operator


    【解决方案1】:

    好吧,delete 仅适用于指针,而您的“图片”是一个对象(除非它以某种方式重载)。此外,由于它是一个本地对象,它应该在 main 的末尾调用析构函数(它应该释放相关的内存,包括加载的图像)。但如果内存需要在GdiplusShutdown(gdiplusToken); 之前释放,您可以调整代码以使用指针:

    Image *picture = new Image (L"TEST.GIF");
    GetEncoderClsid(L"image/bmp", &bmpClsid);
    picture->Save(L"Mosaic2.bmp", &bmpClsid, NULL);
    delete picture;
    

    【讨论】:

    • 这段代码比我的旧代码运行得更快!我一定会把你的名字加到演职员表中。
    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2010-12-05
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2014-10-16
    • 1970-01-01
    相关资源
    最近更新 更多