ImageList: Disposing the original image removes it

2019-02-18 16:02发布

问题:

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?

回答1:

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.



回答2:

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.