-1

In my WPF application, I need to load multiple images to display it in the UI. For that, I am using WriteableBitmap and then pass byte[] into Writeable.Writepixels(). While doing this, I am getting Out of memory exception after 5-6 execution. Image size would be 13-15 MB. I looked the memory consumption in Task manager, in which I noticed that whenever Writepixels() is called memory is drastically increased. kindly refer the below code:

WriteableBitmap bitmap = new WriteableBitmap(img.Width, img.Height, 96, 96, format, null);
var stride = data.Width * ((format.BitsPerPixel + 7) / 8);
bitmap.WritePixels(new Int32Rect(0, 0, data.Width, data.Height), data.Bytes, stride, 0);
return bitmap;

I searched all over the web for 2 days but, I can't find a suitable solution to dispose WriteableBitmap(). Please help me to resolve the solution.

Clemens
  • 123,504
  • 12
  • 155
  • 268
Sathyanarayanan
  • 129
  • 1
  • 1
  • 6
  • 1
    What happens after `return bitmap`? You are certainly using the newly created WriteableBitmap somewhere. You should provide some more details about your application. – Clemens Mar 30 '15 at 11:51
  • After **return bitmap**, I will display the returned image into UI. If the user captures the image from webcam/file mode, again the above piece of code will be executed. in that case, I need to know how to dispose WriteableBitmap. As, WriteableBitmap not belongs to IDisposable interface, it is quite complicated to free the memory. – Sathyanarayanan Mar 30 '15 at 13:22
  • 1
    The fact that it is not disposable just means that it is garbage collected. Then you usually don't have to do any cleanup yourself. In case the bitmaps have the same size each time you create them, you may simply reuse an existing WriteableBitmap instead if creating a new one. If that is not the case, you may still try to freeze them (call their `Freeze` method) in order to reduce memory consumption. – Clemens Mar 30 '15 at 13:37
  • Thanks, but calling **bitmap.Freeze** is not solving the problem. Please provide some other solution to solve the issue. – Sathyanarayanan Mar 31 '15 at 07:06
  • Putting bitmap.freeze does not solve the problem. I already tried to reuse the WriteableBitmap as it globally declared. – Sathyanarayanan Mar 31 '15 at 08:16
  • Global declaration does not automatically mean reusing the existing WriteableBitmap instance. Do not create a `new WriteableBitmap` each time. Create it once, keep the instance as a field in your class (not necessarily static) and call `WritePixels` on that same instance each time. – Clemens Mar 31 '15 at 08:26

1 Answers1

1

Reuse the WriteableBitmap instance instead of creating a new one each time the method is called.

Declare a field in your class to hold the instance:

private WriteableBitmap bitmap;

Then change your method like this:

if (bitmap == null || bitmap.PixelWidth != img.Width || bitmap.PixelHeight != img.Height)
{
    bitmap = new WriteableBitmap(img.Width, img.Height, 96, 96, format, null);
}
var stride = data.Width * ((format.BitsPerPixel + 7) / 8);
bitmap.WritePixels(new Int32Rect(0, 0, data.Width, data.Height), data.Bytes, stride, 0);
return bitmap;
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I implemented the way you mentioned.Thank you, But, the memory is drastically increased when the bytes array is written into the Writeable bitmap. i.e. **bitmap.WritePixels()** – Sathyanarayanan Mar 31 '15 at 09:37