Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用BitmapSource时内存不足_C#_Wpf_Out Of Memory_Bitmapsource - Fatal编程技术网

C# 使用BitmapSource时内存不足

C# 使用BitmapSource时内存不足,c#,wpf,out-of-memory,bitmapsource,C#,Wpf,Out Of Memory,Bitmapsource,我正在以30ms(30fps)的频率在运行时更改WPF映像的源。我的记忆越来越模糊了。 在下面的代码中,iImage是wpf应用程序显示和拥有的私有对象。 字节是在创建窗口时读取和存储一次的字节数组 我怎样才能避免失忆? 是否有更好的解决方案来提高显示原始字节数组的性能 public void LoadBitmapImage(Byte[] bytes, Image iImage) { int bitsPerPixel = 24; double stride = (1024 * bi

我正在以30ms(30fps)的频率在运行时更改WPF映像的源。我的记忆越来越模糊了。 在下面的代码中,iImage是wpf应用程序显示和拥有的私有对象。 字节是在创建窗口时读取和存储一次的字节数组

我怎样才能避免失忆? 是否有更好的解决方案来提高显示原始字节数组的性能

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

使用相同的代码,您可以通过强制垃圾收集来解决内存问题。为此,请将下面的代码放在BitmapSource.Create的上方

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

我不确定是什么导致OutOfMemory异常,但我不会一直重新创建图像。看看哪一个更适合您的需要。谢谢,通过使用可写位图,我避免了内存不足。您可以在调用BitmapSource之前放置
GC.WaitForPendingFinalizers()
。创建,但这更像是一个黑客而不是解决方案。