0

I'm changing source of a WPF image on run time with a frequency of 30ms ( 30 fps ). I'm getting an OutOfMemory. In the following code, iImage is a private object displayed and owned by the wpf application. bytes is a byte array readed and stored once at the creation of the window.

How can I avoid the outOfMemory ? Is there a better solution to have a better performance to display a raw byte array ?

public void LoadBitmapImage(Byte[] bytes, Image iImage)
{  

  int bitsPerPixel = 24;
  double stride = (1024 * bitsPerPixel + 7) / 8;
  BitmapSource wBitmapSource = BitmapSource.Create(1024, 768, 96, 96, PixelFormats.Rgb24, null, bytes , (int)stride);      

  iImage.Source = wBitmapSource;

}

Thks

BuzBuza
  • 607
  • 2
  • 7
  • 19
  • 6
    I'm not sure what cause the OutOfMemory Exception, but i wouldn't recreate the image all the time. Have a look at [WriteableBitmap](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx) which might suit your needs much better. – dowhilefor Sep 13 '11 at 15:01
  • Thanks, by using WritableBitmap I avoid the OutOfMemory. – BuzBuza Sep 13 '11 at 20:44
  • You could place `GC.WaitForPendingFinalizers()` before calling BitmapSource.Create but that's more a hack than a solution. – springy76 Sep 14 '11 at 14:38

1 Answers1

0

With the same code you can solve the memory problem by forcing garbage collection. For that place the below code above BitmapSource.Create.

//force garbage collection
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
Rijul Sudhir
  • 2,064
  • 1
  • 15
  • 19