none
C# System.OutOfMemoryException: 'Out of memory.' RRS feed

  • Question

  • I am using videoSourcePlayer from AForge. now I had to add a function to it because GetCurrentVideoFrame( ) was not working the why I needed. So I make a function called GetCurrent() and it work 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 most be 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 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?

    Thursday, January 3, 2019 11:17 PM

All replies

  • I am using videoSourcePlayer from AForge. now I had to add a function to it because GetCurrentVideoFrame( ) was not working the why I needed. So I make a function called GetCurrent() and it work 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 most be 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 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?


    Friday, January 4, 2019 1:41 AM
  • Maybe you should not execute ‘original.Dispose()’ because the image (probably currentFrame) is still needed later.

    When you perform ‘iOld.Dispose()’, you should probably execute ‘p2.Image = null’ too.

    Friday, January 4, 2019 6:54 AM
  • ok I tried doing both this :

      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;
                bmp.Dispose();
                pictureBox1.Dispose();
                return tempB;
            }
    

    and this :

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

    at the same time but I still get the error 

    Friday, January 4, 2019 1:06 PM
  • Have you printed the rectangle bounds (stIRList and endIRList) to make sure they are reasonable numbers?  Does it fail the very first time, or does it take some number of iterations?  What are you doing with the image when you get it?  Remember that frames come pretty quickly; if you're not eventually disposing of the bitmap you get from GetCurrent, you would run out of memory.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Saturday, January 5, 2019 12:51 AM
  •  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. 

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


    FYI -

    The Help for Bitmap.Clone shows:

    Exceptions

    OutOfMemoryException

    "rect is outside of the source bitmap bounds."

    https://docs.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.clone?view=netframework-4.7.2#System_Drawing_Bitmap_Clone_System_Drawing_RectangleF_System_Drawing_Imaging_PixelFormat_

    - Wayne

    Saturday, January 5, 2019 5:16 AM
  • it fails every time !!!

    here is an example 

    start point X = 135, starting point Y = 294, end point X= 165, end point Y = 327

    Sunday, January 6, 2019 12:16 AM
  • Hi Btb4198,

    Does it throw exception at any of point, or worked at some points? what line of code throws the exception throw the exception, could you please share the detailed error message. 

    In addition, it will be beneficial to resolve the issue if you share a simple reproducible sample (you could create a new simple sample) via OneDrive.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 7, 2019 6:14 AM
    Moderator