Removing GDI+ objects

I am attempting to write a program to randomly select images out of a directory and set them as a desktop background. I know windows has a tool to do this, but I thought it would be interesting to it myself.

I was able to set the background without trouble, but when I set it to loop the memory usage climbs every time an image is loaded and set. Using the task manager I was able to determine that the number of GDI Objects is not increasing though. Below is the function I use to import and resize the images.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void resizeandsave()
{
   Image npic;
   string imgstring;
   std::wstring imgwstring;
   const wchar_t* imgchar;
   int i=0;
   d=opendir(".");
   if (d)
   {
	while ((dir=readdir(d)) != NULL)
	{
		i++;
		dirlist.push_back(dir->d_name);
	}
   	closedir(d);
   }
   int r=genrand_int32()%(i-1);

   imgstring=r;
   imgwstring = std::wstring(img.begin(),img.end())
   imgchar = imgwstring.c_str();
   
   Image pic(imgchar);
   npic = pic.GetThumbnailImage(1680,1050,NULL,NULL);
   npic.Save(L"wallpaper.jpg");
}


I had hoped to just put a couple of deletes at the end of the function of the form
delete pic;
this however gives me the compile error "cannot convert from Gdiplus::Image to void *"

Any suggestions of how to deal with the memory, or to close the Image objects?
... The delete keyword can only be used to delete dynamically allocated memory. And to answer your question. ZeroMemory :D. Or something like that.
Last edited on
Does that even compile?

According to http://msdn.microsoft.com/en-us/library/windows/desktop/ms535394%28v=vs.85%29.aspx GetThumbnailImage returns a pointer (which you own, and therefore must delete.)

Scaling the image would be a better thing to do than creating a large thumbnail image.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms533836%28v=vs.85%29.aspx

Topic archived. No new replies allowed.