释放不是 GDI+ 图像类的成员

dispose is not a member of GDI+ Image class

本文关键字:成员 图像 GDI+ 释放      更新时间:2023-10-16

所以我有一个程序,可以从文件夹中提取随机图像并从中创建拼贴画,并将其设置为Windows壁纸。 这似乎工作正常。 所以我想我会放一个睡眠计时器,让它自动更新,而不必每半小时运行一次。 我这样做了,效果很好,但是我遇到了内存泄漏的问题,在我开始循环之前没有注意到。 我正在尝试释放GDI+对象,但我不断收到错误,即释放不是GDIplus的成员::图像

我正在将图片加载到 Image 对象中,然后调整其大小并将其放入图像数组中,然后我想处理第一个图像。 然后,我想在处理完数组中的图像后处理该数组。

这是使用VS2005的旧副本完成的。

#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <string>
#include <iostream>
#include <vector>
#include <dirent.h>
#include <time.h>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include "cwp05rnd.h"
using namespace Gdiplus;
using namespace std;
#pragma comment (lib,"Gdiplus.lib")
#pragma comment (lib, "user32.lib")
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR           gdiplusToken;
HDC                 hdc;
Graphics            graphics(hdc);
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID jpegClsid;
GetEncoderClsid(L"image/jpeg", &jpegClsid);
SetCurrentDirectoryA("E:\Photos");
ofstream outfile;
outfile.open ("outimgs.txt");
ofstream outfile2;
outfile2.open("imgpos.txt");
srand(time(NULL));
init_genrand(time(NULL));
vector<string>  dirlist;
DIR             *d;
struct          dirent *dir;
int i=0;
d=opendir(".");
if (d)
{
    while ((dir=readdir(d)) != NULL)
    {
        i++;
        dirlist.push_back(dir->d_name);
    }
    closedir(d);
}
Image   wp(L"E:\Dropbox\Photos\wallpaper.jpg");
Graphics* wpimage = Graphics::FromImage(&wp);
int r;
int rvsize=100;
int rv[100]={0};
string img;
std::wstring wimg;
const wchar_t* rimg;
double cwidth;
double cheight;
double ratio;
int nheight;
int counter=0;
    int full = 0;
    int tries = 0;
    int hfull = 0;
    int imgnum =0;
    int last=0;
    Image* newpic[10];
    while ( tries <10)
    {
        redo:
        tries++;
        int newrv=0;
        while (newrv ==0)
        {
            r=genrand_int32()%i;
            for (int k=0; k < rvsize; k++)
            {
                if (rv[k] > 0 && rv[k]==r )
                {
                    break;
                }
                if (rv[k]==0 && r < i)
                {
                    newrv =1;
                    rv[k]=r;
                    last=k;
                    break;
                }
                if (rv[k] ==0)
                {
                    break;
                }
            }
        }
        img = dirlist[r];
        if (img[0]=='.')
        {
            newrv=0;
            goto redo;
        }
        wimg = std::wstring(img.begin(),img.end());
        rimg = wimg.c_str();
        Image pic(rimg);
        cwidth = pic.GetWidth();
        cheight = pic.GetHeight();
        if (cheight ==0)
        {
            outfile2 << "error" << img << endl;
            rv[last]=0;
            system("pause");
            goto redo;
        }
        ratio = cwidth/cheight;
        nheight = nwidth/ratio;
        pic.RotateFlip(Rotate180FlipNone);
        pic.RotateFlip(Rotate180FlipNone);
        newpic[imgnum] = pic.GetThumbnailImage(nwidth,nheight,NULL,NULL);
                    delete pic[0];
                    imgnum = imgnum + 1;
        }

然后,根据各种随机值,NewPic中的图像上有一长段翻转和旋转。

wpimage->DrawImage(newpic[k],(j*nwidth),(((k+1)*whitespace)+htot),nwidth,nh[k]);
wp.Save(L"C:\Temp\wallpaper\nwallpaper.jpg", &jpegClsid, NULL);
delete newpic;
setWall();
delete wpimage;
delete wp;
return 0;
}

当我尝试删除 Image 对象时,出现错误,指出它无法删除不是指针的对象,或者无法从 GDIplus::Image 转换为 void*

任何建议将不胜感激。

我注意到你有Image pic(rimg);但你正在做delete pic[0]; pic不是指针。不是动态分配什么的...也不是数组(或者也许是......但直觉上我认为不是。

*加*哦,是的,如果您已经解决了这个问题,建议您关闭问题或至少提及它......