【问题标题】:Out of memory when using BitmapSource使用 BitmapSource 时内存不足
【发布时间】: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


【解决方案1】:

使用相同的代码,您可以通过强制垃圾回收来解决内存问题。对于那个地方下面的代码上面BitmapSource.Create。

//force garbage collection
System.GC.Collect();
System.GC.WaitForPendingFinalizers();

【讨论】:

    猜你喜欢
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多