Search code examples
c#.net-4.5windows-store

How to make BitmapImage disposable


I need to use a BitmapImage in a using statement, how could this be done?

using (BitmapImage bitmap = new BitmapImage())
{
    ...

I am guessing the way to do it is by extending IDisposable but I have never done that before.

Thanks in advance


Solution

  • You can't do it. BitmapImage is sealed so you can't derive from it. Furthermore, I don't know why you would implement IDisposable in a .NET Framework class. The most you could do is a wrapper class which would contain your BitmapImage, and implement IDisposable.

    Example:

    class DisposableBitmapImageWrapper : IDisposable
    {
        public BitmapImage Bitmap { get; private set; }
    
        public DisposableBitmapImageWrapper(BitmapImage bitmap)
        {
            Bitmap = bitmap;
        }
    
        public void Dispose()
        {
            //Do something with the BitmapImage
        }
    }