I'm in the process of trying to be sure I have everything disposed. I
have always assumed that if you create the image, you must dispose it.
Is this true if I'm storing the image in an ImageList? It appears
that ImageList *might* do the dispose for you. If so, are there any
other exceptions to the general rule that if you create it you should
dispose it?
Thanks.
Bob Powell [MVP] - 27 Jan 2008 14:19 GMT
If the ImageList owns the images it will dispose of them, if it doesn't you
should.

Signature
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
> I'm in the process of trying to be sure I have everything disposed. I
> have always assumed that if you create the image, you must dispose it.
[quoted text clipped - 5 lines]
>
> Thanks.
JS - 27 Jan 2008 15:07 GMT
Can you explain what 'owns the images' means? What if I do
imageList.Add(new Bitmap(100,100)) ? Does the image list own this
bitmap?
Thanks for the help.
Bob Powell [MVP] - 27 Jan 2008 16:57 GMT
Nope, you own it. Technically It's your responsibility to dispose of it.
However.. In reality, if you create the bitmaps and ImageLists in the
initialization of the application and don't bother to dispose of them when
the app dies the images will be destroyed just fine.
Similarly if you dispose of the ImageList then it will not dispose of the
images but the references to the individual images may all have died so they
will eventually be reclaimed by the garbage collector which in turn will see
that the object implements the dispose pattern so eventually the images will
be reclaimed.
If you are creating and destroying a lot of ImageLists during the lifetime
of the application then you should dispose of the images corectly just
because it will save you memory problems. If is't just a normal usage like
for menu items that live for the whole application run then don't bother.

Signature
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
> Can you explain what 'owns the images' means? What if I do
> imageList.Add(new Bitmap(100,100)) ? Does the image list own this
> bitmap?
>
> Thanks for the help.
Rudy Velthuis - 27 Jan 2008 17:04 GMT
> Can you explain what 'owns the images' means? What if I do
> imageList.Add(new Bitmap(100,100)) ? Does the image list own this
> bitmap?
No. It makes a copy of the data of the bitmap and stores that in its
internal bitmap.

Signature
Rudy Velthuis http://rvelthuis.de
"I don't feel good."
-- The last words of Luther Burbank (1849-1926)
Rudy Velthuis - 27 Jan 2008 17:01 GMT
> I'm in the process of trying to be sure I have everything disposed. I
> have always assumed that if you create the image, you must dispose it.
>
> Is this true if I'm storing the image in an ImageList? It appears
> that ImageList might do the dispose for you.
The imagelist makes a copy of the image on an internal bitmap. The
imagelist will take care of getting rid of that internal bitmap.

Signature
Rudy Velthuis http://rvelthuis.de
"If the brain were so simple we could understand it, we would
be so simple we couldn't." -- Lyall Watson