10

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")))
{
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Andrew Keith
  • 7,515
  • 1
  • 25
  • 41
  • [Related](https://stackoverflow.com/questions/8352787/how-to-free-the-memory-after-the-bitmapimage-is-no-longer-needed) – Goufalite Sep 15 '17 at 10:58

1 Answers1

14

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • ok. My colleagues mentioned that WPF is fully dx accelerated and images , etc are stored in video. Is this just a fantasy ? I am a long time winforms programmer and am now writing a new module in WPF so i assumed i needed to dispose of images. – Andrew Keith Oct 20 '09 at 01:44
  • 1
    A BitmapSource isn't an image - it's used to generate one. However, it's a managed pixel storage medium. Kind of confusing, but it's always worth checking. In general, just look at the class hierarchy - if it doesn't implement IDisposable, you're free to let the GC worry instead of you. :) – Reed Copsey Oct 20 '09 at 15:21
  • 1
    Except it does, if it's a WritableBitmap. – Glenn Maynard May 18 '14 at 04:13