C# System.OutOfMemoryException:&x27;内存不足;

C# System.OutOfMemoryException:&x27;内存不足;,c#,video,memory-leaks,out-of-memory,C#,Video,Memory Leaks,Out Of Memory,更新 我正在使用来自AForge的videoSourcePlayer。现在我必须给它添加一个函数,因为 GetCurrentVideoFrame()没有按我需要的方式工作。因此,我创建了一个名为GetCurrent()的函数,它按照我想要的方式工作。我遇到的问题是,当我用它代替GetCurrentVideoFrame()时,我得到一个System.OutOfMemoryException:“内存不足”。我不知道为什么。 这是我的密码: Bitmap getPic2(int i2) {

更新

我正在使用来自AForge的videoSourcePlayer。现在我必须给它添加一个函数,因为
GetCurrentVideoFrame()
没有按我需要的方式工作。因此,我创建了一个名为
GetCurrent()
的函数,它按照我想要的方式工作。我遇到的问题是,当我用它代替
GetCurrentVideoFrame()
时,我得到一个System.OutOfMemoryException:“内存不足”。我不知道为什么。 这是我的密码:

Bitmap getPic2(int i2)
    {
        Bitmap bmp = null;
        Bitmap tempB = null;
        if (endIRList[i2].X > videoSourcePlayer.Width - 1)
            endIRList[i2]= new System.Drawing.Point(videoSourcePlayer.Width - 1, endIRList[i2].Y);
        if (endIRList[i2].Y > videoSourcePlayer.Height - 1)
            endIRList[i2] = new System.Drawing.Point(endIRList[i2].X, videoSourcePlayer.Height - 1);
        if (stIRList[i2].X >= 0 && stIRList[i2].Y >= 0 && endIRList[i2].X < videoSourcePlayer.Width && endIRList[i2].Y < videoSourcePlayer.Height)
        {
            if (endIRList[i2].X - stIRList[i2].X > 0 && endIRList[i2].Y - stIRList[i2].Y > 0)
            {
                bmp = videoSourcePlayer.GetCurrent();
                System.Drawing.Image iOld = p2.Image;
                tempB = bmp.Clone(new Rectangle(stIRList[i2].X, stIRList[i2].Y, endIRList[i2].X - stIRList[i2].X, endIRList[i2].Y - stIRList[i2].Y),bmp.PixelFormat);

                if (iOld != null)
                {
                    iOld.Dispose();
                    iOld = null;
                }
            }
        }
        pictureBox1.Image =this.videoSourcePlayer.GetCurrent();

        TestPicBox.Image = tempB;


        return tempB;
    }
现在,如果我只使用
bmp=GetCurrentVideoFrame
,我就不会遇到问题。所以我的函数
GetCurrentVideo

代码如下:

  public Bitmap GetCurrentVideoFrame( )
    {
        lock ( sync )
        {
            return ( currentFrame == null ) ? null : AForge.Imaging.Image.Clone( currentFrame );
        }
    }

      public Bitmap GetCurrent()
    {
        lock (sync)
        {
            Bitmap currentPic = null;
            Bitmap original = GetCurrentVideoFrame();
            currentPic = new Bitmap(original, new Size(original.Width / 2, original.Height / 2));
            original.Dispose();
            original = null;
            return currentPic;

        }

    }
我就是不明白为什么它们的功能有效,而我的功能无效。
有人能帮忙吗

简而言之,您的程序是一个大型高效非托管GDI内存泄漏

如果创建或克隆了位图,则需要在某个阶段对其进行处理(使用
dispose
方法)。这,你不是在做什么

tempB
(顺便说一句,命名非常糟糕)和
bmp
都需要在某个时候进行处理

你不能希望他们离开,也不能忽视他们


提示,如果您使用位图或非托管资源,请特别注意何时何地使用它,并确保正确处置它

简而言之,您的程序是一个大型高效非托管GDI内存泄漏

如果创建或克隆了位图,则需要在某个阶段对其进行处理(使用
dispose
方法)。这,你不是在做什么

tempB
(顺便说一句,命名非常糟糕)和
bmp
都需要在某个时候进行处理

你不能希望他们离开,也不能忽视他们


提示如果使用位图或非托管资源,请特别注意何时何地使用它,并确保其正确处理

类似的线程可以在此处找到-您必须使用正确的清理和处理方法。类似线程的可能副本可以在此处找到-您必须使用正确的清理和处理方法。确定的可能副本我不确定在何处添加,原始版本有一个。我会把这张照片还给你,这样我就可以做那张了。在我使用bmp之前,它就死掉了。@brandon您要创建的矩形的大小是多少?如果这个值太大,你也会遇到同样的问题,我会动态设置它,但是当我使用它们的函数GetCurrentVideoFrame()时,我从来没有遇到过这个错误。无论矩形有多大或多小,图片的内容是否也需要处理?@brandon完全回避了这个问题,你需要给我至少一次它损坏的测量值。我不知道在哪里添加它,原始的有一个。我会把这张照片还给你,这样我就可以做那张了。在我使用bmp之前,它就死掉了。@brandon您要创建的矩形的大小是多少?如果这个值太大,你也会遇到同样的问题,我会动态设置它,但是当我使用它们的函数GetCurrentVideoFrame()时,我从来没有遇到过这个错误。无论矩形有多大或多小,
pictureBox1.Image
的内容是否也需要处理?@brandon完全回避了这个问题,你需要给我至少一次它断裂的测量值
  public Bitmap GetCurrentVideoFrame( )
    {
        lock ( sync )
        {
            return ( currentFrame == null ) ? null : AForge.Imaging.Image.Clone( currentFrame );
        }
    }

      public Bitmap GetCurrent()
    {
        lock (sync)
        {
            Bitmap currentPic = null;
            Bitmap original = GetCurrentVideoFrame();
            currentPic = new Bitmap(original, new Size(original.Width / 2, original.Height / 2));
            original.Dispose();
            original = null;
            return currentPic;

        }

    }