Is it safe to dispose the Icon after calling Icon.ToBitmap()?

為{幸葍}努か 提交于 2019-12-10 21:04:52

问题


After calling System.Drawing.Icon.ToBitmap() to create an image, is it safe to dispose the original Icon?


回答1:


Yes. Icon.ToBitmap draws the Icon to a new Bitmap object so it is safe to dispose it afterwards.

Edit:
Looking at the Icon.ToBitmap() method in Reflector was interesting. I expected it to be a simple Graphics.DrawImage or Graphics.DrawIcon call but it is more involved than that. As long as it is possible the function will do a memory copy of the icon image data instead, but it will revert to a Graphics.DrawImage or Graphics.DrawIcon call if it cannot perform the copy. The memory copy is much faster so that is obviously the reason, but this makes the code much harder to read.




回答2:


The method converts an Icon to a new Bitmap object, so there will be no reference from the Bitmap to the Icon.

So yes, it is safe to dispose the Icon.




回答3:


Yes. If you don't need the icon any more, and have the bitmap stored somewhere, you're fine.



来源:https://stackoverflow.com/questions/564574/is-it-safe-to-dispose-the-icon-after-calling-icon-tobitmap

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