views:

219

answers:

3

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

A: 

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

Inferis
+6  A: 

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.

Gerrie Schenck
+3  A: 

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.

Rune Grimstad