【发布时间】:2011-09-13 14:47:36
【问题描述】:
我正在以 30 毫秒 ( 30 fps ) 的频率在运行时更改 WPF 图像的来源。我得到了 OutOfMemory。 在以下代码中,iImage 是 wpf 应用程序显示和拥有的私有对象。 bytes 是在创建窗口时读取并存储一次的字节数组。
如何避免 outOfMemory ? 有没有更好的解决方案可以更好地显示原始字节数组?
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;
}
感谢
【问题讨论】:
-
我不确定是什么导致了 OutOfMemory 异常,但我不会一直重新创建图像。看看WriteableBitmap,它可能更适合您的需求。
-
谢谢,通过使用 WritableBitmap 我避免了 OutOfMemory。
-
您可以在调用 BitmapSource.Create 之前放置
GC.WaitForPendingFinalizers(),但这更像是一种技巧而不是解决方案。
标签: c# wpf out-of-memory bitmapsource