tags:

views:

208

answers:

2

I have developed a WPF Application, using BitmapDecoder to save images. While saving images i am getting an "insufficient memory to complete the operation" exception.

The code looks something like this:

BitmapDecoder imgDecoder = BitmapDecoder.Create(mem,
    BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.None);

I think BitmapDecoder object may be the cause of that exception; how do I dispose of the object?

+1  A: 

BitmapDecoder is not disposable. Just make sure you don't keep any reference to the BitmapDecoder if you don't need it anymore, and the GC will do its job and collect the unused memory when needed.

Thomas Levesque
Thanks for the reply.
ibrahimkhan
A: 

I ran into the same issue. I have an app that loads thousands of images using the BitmapDecoder and was hitting memory problems. I had to create a wrapper class ImageFileHandler that handled all interaction with the BitmapDecoder, then I stored my instance of BitmapDecoder in a WeakReference. So if the OS needed the memory, my weak reference would give up the BitmapDecoder, and then every time my ImageFileHandler needed it, it would create a new one if necessary.

Turbo