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 on开发者_如何学JAVAce 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
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();
精彩评论