gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:44:18 24 4
gpt4 key购买 nike

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

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

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

我的代码:

#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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com