2011-09-13 52 views
0

我在運行時更改了30毫秒(30 fps)頻率的WPF圖像源。我得到一個OutOfMemory。 在以下代碼中,iImage是由wpf應用程序顯示和擁有的私有對象。 字節是一個字節數組,在創建窗口時被一次一次地存儲和存儲。使用BitmapSource時內存不足

如何避免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; 

} 

THKS

+5

我不確定是什麼導致OutOfMemory異常,但我不會一直重新創建映像。看看[WriteableBitmap](http://msdn.microsoft.com/zh-cn/library/system.windows.media.imaging.writeablebitmap.aspx),它可能更適合您的需求。 – dowhilefor

+0

謝謝,通過使用WritableBitmap我避免了OutOfMemory。 – BuzBuza

+0

在調用BitmapSource.Create之前,您可以放置​​'GC.WaitForPendingFinalizers()',但這更像是一種解決方案。 – springy76

回答

0

用同樣的代碼,你可以通過強制垃圾收集解決了內存不足的問題。對於那個地方,下面的代碼在BitmapSource.Create以上。

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