2013-07-02 59 views
0

我捕捉屏幕,然后将图像数据保存到内存流,以便我可以将其设置为BitmapImage对象。C#WPF处理图像对象

bmp = new System.Drawing.Bitmap((int)sel.rectSelectedArea.Width, (int)sel.rectSelectedArea.Height); 
bounds = new System.Drawing.Rectangle((int)sel.rectSelectedArea.X, (int)sel.rectSelectedArea.Y, (int)sel.rectSelectedArea.Width, (int)sel.rectSelectedArea.Height); 

using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) 
    g.CopyFromScreen(new System.Drawing.Point(bounds.Left, bounds.Top), System.Drawing.Point.Empty, bounds.Size); 

ms = new MemoryStream(); 
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
ms.Position = 0; 

frames.Add(new BitmapImage()); 
frames.Last().BeginInit(); 
frames.Last().StreamSource = ms; 
frames.Last().EndInit(); 
frames.Last().Freeze(); 

然后,当我需要显示该框架给用户。我将选定的帧设置为图像对象的来源。

imgExample.Source = frames[targetFrame]; 

问题是当我向用户显示另一帧时。前一帧保留在内存中,所以在大约200帧之后,它会消耗从未释放的3-600,000 K的内存。

有没有一种方法让imgExample(Image对象)立即释放内存? 或者是否有覆盖相同内存的方法,而不是为所有帧创建新对象。

+1

我爱你带来的恐龙'System.Drawing'东西到WPF的方式。 –

+0

你可以尝试一个WriteableBitmap - 这些很容易更新,只要你喜欢。 – Chris

回答

0
bmp.Dispose(); 
bmp = null; 

GC.Collect(); 
+0

我需要保留用于显示框架的BMP,但是我不需要将它显示在内存中。 – John

+0

所以,你想保持BMP ...但不是在内存中?说,通过写入磁盘? –