ImageList: Disposing the original image removes it from the list

て烟熏妆下的殇ゞ 提交于 2019-12-01 08:54:47

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!