c++ - 无法删除 GDI+ 图像

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

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

type 'class Gdiplus::Image' argument given to 'delete', expected pointer

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

我的代码:

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

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

最佳答案

嗯,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;

关于c++ - 无法删除 GDI+ 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23711288/

相关文章:

c++ - SDL:制作静态文本变量

c++ - Visual Studio 强制桌面 API

windows - IOCP : how does the kernel decide to complete WSASend synchronously or asynchronously?

c++ - 在 Win32 API ListView 中添加带换行符的文本

android - 如何在 Android 中拍照、保存和获取照片

java - 在相机预览中计算颜色

c++ - Qt 无法使 QEditLine 显示文本

c++ - 在成员函数中我得到错误 "invalid use of undefined type ' struct (name )' - forward declaration of ' struct (name )' "

c++ - 在 googletest 中循环测试用例

Android copyPixelsFromBuffer() 到特定位图偏移量?