Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to dispose a BitmapSource

How are you supposed to dispose of a BitmapSource ?

// this wont work because BitmapSource doesnt implement IDisposable
using(BitmapSource bitmap = new BitmapImage(new Uri("myimage.png")))
{
}
like image 220
Andrew Keith Avatar asked Oct 20 '09 01:10

Andrew Keith


People also ask

How do you dispose of system drawing images?

Call Dispose when you are finished using the Image. The Dispose method leaves the Image in an unusable state. After calling Dispose, you must release all references to the Image so the garbage collector can reclaim the memory that the Image was occupying.

Do I need to dispose bitmap?

You should dispose it after you have finished with it in the code where this method is called and ignore the warning. Actually, further investigation has revealed that warning to be not as ridiculous as it sounds. It's telling you that bitmap won't be disposed if there is an exception before the method returns.


1 Answers

You do not have to Dispose() a BitmapSource. Unlike some other "image" classes in the Framework, it does not wrap any native resources.

Just let it go out of scope, and the garbage collector will free its memory.

like image 93
Reed Copsey Avatar answered Oct 08 '22 23:10

Reed Copsey