0

我捕获屏幕,然后将图像数据保存到内存流中,以便将其设置为 BitmapImage 对象。

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(图像对象)立即释放内存?或者是否有一种方法可以覆盖相同的内存,而不是为所有帧创建新对象。

4

1 回答 1

0
bmp.Dispose();
bmp = null;

或者

GC.Collect();
于 2013-07-02T17:40:05.390 回答