Bitmap clone out of memory RRS feed

  • Question

  • I can draw a blue box around an area on my video player and clone that part of the picture. however, it seems that if I get to close to the end of the picture I get an out of memory exception. However, I am not that close to the  edges, and over all, I should never be getting that exception, because I am drawing  inside the video player...

    here is my code:

      // alignment camera's dimensions with the video player
            private Bitmap alignment()
                // making a new Bitmap using the video player's dimensions
                Bitmap tempBitmap = new Bitmap(videoSourcePlayer.Width, videoSourcePlayer.Height);
                // making a new Graphic from that bitmap
                Graphics g = Graphics.FromImage(tempBitmap);
                // getting picture from the camera
                Bitmap originalBmp = videoSourcePlayer.GetCurrentVideoFrame();
                while (originalBmp == null)
                    originalBmp = videoSourcePlayer.GetCurrentVideoFrame();
                //making new  bitmap using the  Graphics g 
                g.DrawImage(originalBmp, 0, 0, videoSourcePlayer.Width, videoSourcePlayer.Height);
                // returning the new bitmap 
                return tempBitmap;

      Bitmap getPic2() // I removed the int i2, you did not need it at a  parameter, you did not need it
                Bitmap bmp = null;
                Bitmap tempB = null;
                // getting the current image from the video player
                bmp = alignment();//videoSourcePlayer.GetCurrentVideoFrame();
                // if somehow the box is bigger than the video Source Player control than just used the  
                // videoSourcePlayer Width and hight  
                if (_endIR.X > videoSourcePlayer.Width - 1)
                    _endIR = new System.Drawing.Point(videoSourcePlayer.Width - 1, _endIR.Y);
                if (_endIR.Y > videoSourcePlayer.Height - 1)
                    _endIR = new System.Drawing.Point(_endIR.X, videoSourcePlayer.Height - 1);
                if (_stIR2.X >= 0 && _stIR2.Y >= 0 && _endIR.X < videoSourcePlayer.Width && _endIR.Y < videoSourcePlayer.Height)
                    //if (_endIR.X - _stIR2.X >= 0 && _endIR.Y - _stIR2.Y >= 0)
                    //Taking the dimensions from the box and using that to cut out a new image from the current video stream
                    int recW = Math.Abs( _endIR.X - _stIR2.X);
                    int recH = Math.Abs(_endIR.Y - _stIR2.Y);
                        tempB = bmp.Clone(new Rectangle(_stIR2.X, _stIR2.Y, recW, recH), bmp.PixelFormat);
                        if (bmp != null)
                            // free up the memory 
                            bmp = null;
                return tempB;

    his exception at all.

    here is my code :

    Wednesday, December 4, 2019 8:37 PM

All replies

  • Make sure that Rectangle is valid. Use Debugger to check the coordinates and sizes before calling Clone.

    Wednesday, December 4, 2019 9:09 PM
  • Hi Btb4198,

    Thank you for posting here.

    Since this thread is related to AForge.NET, it is a third-party product, we only provide support for Microsoft related products.

    I suggest that you can ask this question in their forum.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,


    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.

    Thursday, December 5, 2019 6:03 AM