我捕捉屏幕,然后将图像数据保存到内存流,以便我可以将其设置为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对象)立即释放内存? 或者是否有覆盖相同内存的方法,而不是为所有帧创建新对象。
我爱你带来的恐龙'System.Drawing'东西到WPF的方式。 –
你可以尝试一个WriteableBitmap - 这些很容易更新,只要你喜欢。 – Chris