【发布时间】:2013-04-09 10:53:03
【问题描述】:
我正在用 C# 开发一个应用程序,并且我开发了一个库来处理 Aforge 相机的一些事情。要点之一是简单地捕获网络摄像头前的图像并将其显示在特定的PictureBox 上:
camera.NewFrame += NewFrame;
private void NewFrame(object sender, NewFrameEventArgs args)
{
Bitmap newFrame = new Bitmap(args.Frame);
args.Frame.Dispose();
PictureBox.FromBitmapImage(newFrame);
newFrame.Dispose();
newFrame = null;
}
我在这里做什么,我把每一帧都画到PictureBox。
我的疑问是:
在某些计算机中,这种绘制方式会产生非常高的内存泄漏。摄像头配置为:640x480,如果更高,内存泄漏会增加。
电脑配置:
Intel i5:内存泄漏到 500Mb
Intel i7:没有内存泄漏。
双心(没那么强大):没有那么多内存泄漏。
编辑:
public static void FromBitmapImage(this Image image, Bitmap bitmap)
{
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, ImageFormat.Bmp);
memoryStream.Position = 0;
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}
image.Source = bitmapImage;
bitmapImage = null;
}
我不明白为什么我在某些计算机中会出现内存泄漏,而在其他计算机中却没有...请问有什么建议吗?
注意:内存泄漏仅在 Visual Studio 2010 的发布模式下发生,而不是在调试时发生。
NOTE2:我认为问题出在FromBimapImage,因为我尝试了WindowsForms 应用程序而不是WPF 应用程序并且没有内存泄漏...
【问题讨论】:
-
PictureBox.FromBitmapImage(Bitmap)方法是什么,它返回什么?
标签: memory-leaks camera aforge