LTR LTR - 10 months ago 56
C# Question

ImageList: Disposing the original image removes it from the list

ImageList should create a copy of all images that are inserted into it. Therefore it should be safe to dispose the originals after adding them to the list.

Why does the following testcase fail?

Bitmap test = new Bitmap(128, 128);
ImageList il = new ImageList();

Assert.AreEqual(1, il.Images.Count); // OK, image has been inserted
test.Dispose(); // now let's dispose the original
var retrievalTest = il.Images[0];
catch (ArgumentException) // ... but this Exception happens!

Assert.AreEqual(1, il.Images.Count); // and this will fail

What seems to happen here is this: When trying to retrieve the image, the ImageList discovers that the original has been disposed, and removes it from the ImageList.

Why is that happen, I thought the ImageList is supposed to create a copy of the image?

Answer Source

Yes, ImageList creates a copy of the bitmap. But your test code runs afoul of the famous lazy initialization pattern that's so common in the .NET framework. What matters is when it creates the copy. Which is does only when it has to. Make a small change in your code to hurry that up:

var dummy = il.Handle;     // <== NOTE: added
test.Dispose();            // no problem

And you'll see that disposing is no longer a problem.

Not sure how to give proper advice here, the code is too synthetic. This in general works well enough, ImageList makes the copy when its consumers start using its bitmaps, Treeview or ListView. In general, avoid using ImageList as a collection object, it wasn't made to do that job. Separate the view from the model and you'll stay out of trouble.