Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();

il.Images.Add(test);
Assert.AreEqual(1, il.Images.Count); // OK, image has been inserted
test.Dispose(); // now let's dispose the original
try
{
    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?

like image 731
LTR Avatar asked Sep 03 '25 06:09

LTR


2 Answers

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:

il.Images.Add(test);
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.

like image 60
Hans Passant Avatar answered Sep 04 '25 21:09

Hans Passant


ImageList should create a copy of all images that are inserted into it.

I don't see any indication in the documentation, that it does. So the simple answer is: your assumption is wrong.

like image 35
nvoigt Avatar answered Sep 04 '25 22:09

nvoigt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!