0

update

I am using videoSourcePlayer from AForge. now I had to add a function to it because GetCurrentVideoFrame( ) was not working the way I needed. So I make a function called GetCurrent() and it works the way I wanted. The problem I am having is when I used it in place of GetCurrentVideoFrame( ) I get a System.OutOfMemoryException: 'Out of memory.' Exception and I have no Ideal why. here is my code :

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

the problem I am having is at:

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

now if I just use bmp = GetCurrentVideoFrame I do not get the problem. so something must is wrong with my function GetCurrentVideo

here is the code :

  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;

        }

    }

I just cant see why their function works and my does not. can anyone help?

brandon
  • 73
  • 10
  • Similar thread can be found here - https://stackoverflow.com/questions/199468/c-sharp-image-clone-out-of-memory-exception You have to use proper clean up and disposing methods. – Dharmashree Jan 03 '19 at 23:23
  • Possible duplicate of [Picture Box out of Memory](https://stackoverflow.com/questions/6991157/picture-box-out-of-memory) – mjwills Jan 04 '19 at 00:41

1 Answers1

4

In short, your program is one large efficient unmanaged GDI memory leak

If a bitmap gets created or cloned, you need to dispose it at some stage (using the Dispose method). This, you are not doing

Both tempB (seriously bad naming by the way) and bmp need to be disposed at some point.

You can't wish them away, or ignore them.

Tip, if you play with a bitmap, or unmanaged resource, pay special attention to when and where it gets used, and make sure it's disposed of properly.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141