AForge相机内存泄漏 [英] AForge camera memory leak

查看:1019
本文介绍了AForge相机内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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;
}

我在这里做了,我得到每一帧, c $ c> PictureBox 。



我的疑问是



在某些计算机中,这种绘画方式会产生很高的内存泄漏。相机配置为: 640x480 ,如果较高,则内存泄漏增加。



计算机配置:



Intel i5:内存泄漏到500Mb



Intel i7:

Double coeur(不那么强大):没有太多的内存泄漏。



EDIT:

  public static void FromBitmapImage(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的发布模式,而不是调试。 / p>

注意:我认为问题出在 FromBimapImage code> WindowsForms 应用程序而不是 WPF 一个,没有内存泄漏...

解决方案

AForge 拥有图片的字节,您应该自己复制。将帧传递给Bitmap构造函数是不够的。



尝试使用:


如果你没有正确处理位图,

  private void NewFrame(object sender,NewFrameEventArgs args)
{
Bitmap newFrame = AForge.Imaging.Image.Clone(args.Frame) ;
PictureBox.FromBitmapImage(newFrame);
newFrame.Dispose();
}


I'm developing an application in C#, and I developed a library that does some stuff with Aforge cameras. One of the points is to simply capture images of what is in front of the Web Cam and show it on a specific PictureBox to do so:

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;
    }

What I do here, I get every frame and paint it into the PictureBox.

My doubt is:

In some computers, this way of painting produces a really high memory leak. The camera configuration is: 640x480, and if it's higher, memory leak increases.

Computer configuration:

Intel i5: memory leak to 500Mb

Intel i7: NO memory leak.

Double coeur (not so powerful): Not so much memory leak.

EDIT:

    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;
    }

I don't understand why I have memory leak in some computers and others not... Any suggestion please?

NOTE: That memory leaks only happen in Release mode on Visual Studio 2010, but not on debug.

NOTE2: I think the problem comes with the FromBimapImage, because I tried a WindowsForms application instead of a WPF one and no memory leak...

解决方案

AForge owns the bytes of the image, you should make your own deep copy. Passing the frame to the Bitmap constructor is not enough. If the framework is not able to properly dispose the bitmap because you have a reference to it, there's a leak.

Try with this:

private void NewFrame(object sender, NewFrameEventArgs args)
{
    Bitmap newFrame = AForge.Imaging.Image.Clone(args.Frame);
    PictureBox.FromBitmapImage(newFrame);
    newFrame.Dispose();
}

这篇关于AForge相机内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆