Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
public class CropHandler
    {
        ImageHandler imageHandler;
        private Bitmap _bitmapPrevCropArea;

        public CropHandler(ImageHandler imageHandler)
        {
            this.imageHandler = imageHandler;
        }

        public void Crop(int xPosition, int yPosition, int width, int height)
        {
            Bitmap temp = (Bitmap)imageHandler.CurrentBitmap;
            Bitmap bmap = (Bitmap)temp.Clone();
            if (xPosition + width > imageHandler.CurrentBitmap.Width)
                width = imageHandler.CurrentBitmap.Width - xPosition;
            if (yPosition + height > imageHandler.CurrentBitmap.Height)
                height = imageHandler.CurrentBitmap.Height - yPosition;
            Rectangle rect = new Rectangle(xPosition, yPosition, width, height);
            imageHandler.CurrentBitmap = (Bitmap)bmap.Clone(rect, bmap.PixelFormat);
        }

        public void DrawOutCropArea(int xPosition, int yPosition, int width, int height)
        {
            imageHandler.RestorePrevious();
            _bitmapPrevCropArea = (Bitmap)imageHandler.CurrentBitmap;
            Bitmap bmap = (Bitmap)_bitmapPrevCropArea.Clone();
            Graphics gr = Graphics.FromImage(bmap);
            Brush cBrush = new Pen(Color.FromArgb(150, Color.White)).Brush;
            Rectangle rect1 = new Rectangle(0, 0, imageHandler.CurrentBitmap.Width, yPosition);
            Rectangle rect2 = new Rectangle(0, yPosition, xPosition, height);
            Rectangle rect3 = new Rectangle(0, (yPosition + height), imageHandler.CurrentBitmap.Width, imageHandler.CurrentBitmap.Height);
            Rectangle rect4 = new Rectangle((xPosition + width), yPosition, (imageHandler.CurrentBitmap.Width - xPosition - width), height);
            gr.FillRectangle(cBrush, rect1);
            gr.FillRectangle(cBrush, rect2);
            gr.FillRectangle(cBrush, rect3);
            gr.FillRectangle(cBrush, rect4);
            imageHandler.CurrentBitmap = (Bitmap)bmap.Clone();
        }

        public void RemoveCropAreaDraw()
        {
            imageHandler.CurrentBitmap = (Bitmap)_bitmapPrevCropArea.Clone();
        }

       public Bitmap cropImage(Rectangle _selection)
        {
            try
            {

                int cropWidth = 0;
                int cropHeight = 0;
                int cropXPosition = 0;
                int cropYPosition = 0;

                cropWidth = objCurrImageHandler.CurrentBitmap.Width;
                cropHeight = objCurrImageHandler.CurrentBitmap.Height;

                cropXPosition = _selection.X;
                cropYPosition = _selection.Y;
                cropWidth = _selection.Width;
                cropHeight = _selection.Height;

                if (cropWidth != 0 && cropHeight != 0)
                {
                    objCurrImageHandler.CurrentCropHandler.DrawOutCropArea(cropXPosition, cropYPosition, cropWidth, cropHeight);

                    if (MessageBox.Show("Do u want to crop this area?", "ImageProcessing", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {

                        objCurrImageHandler.CurrentCropHandler.Crop(cropXPosition, cropYPosition, cropWidth, cropHeight);
                        //objOriginalCurrImgHandler.CurrentBitmap = objCurrImageHandler.CurrentBitmap;
                    }
                    else
                    {
                        objCurrImageHandler.CurrentCropHandler.RemoveCropAreaDraw();
                    }
                }
               

            }
            catch (Exception ex)
            {
               // MessageBox.Show("Error occured: " + ex.Message, "Image Processing", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return objCurrImageHandler.CurrentBitmap;
      }


This program shows "Out of memory exception". I have tried to dispose disposable objects but still it shows the exception.
Could anybody please optimize the code?
Posted
Comments
[no name] 19-Apr-12 6:58am    
Are we supposed to guess which line is throwing the exception?
E.F. Nijboer 19-Apr-12 7:10am    
Step through the code using the debugger and watch all the variables. Probably the size of the bitmap you try to create is becomming to large.
Ed Nutting 19-Apr-12 9:03am    
Right on the money with this answer I think. "...probably the size of the bitmap..." - From the looks of it he's calling Clone() on his bitmaps at least 5 times! It's no wonder it's running out of memory since half those clones don't appear to be disposed at all (and they almost certainly aren't necessary if this were done properly). He's got far more than just one bitmap in memory along with graphics objects and other high memory consuming objects that are never properly disposed (e.g. cBrush).
Vipin_Arora 19-Apr-12 8:41am    
Debug your code with F11

1 solution

This looks like a horribly over complex way of cropping an image, especially given the example I have found. Please take a look at the section on the following page that has a nice short and simple snippet of code for cropping images:
http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing[^]

Specifically this snippet of code: (taken from original source above)
C#
private static Image cropImage(Image img, Rectangle cropArea)
{
   Bitmap bmpImage = new Bitmap(img);
   Bitmap bmpCrop = bmpImage.Clone(cropArea,
   bmpImage.PixelFormat);
   return (Image)(bmpCrop);
}


Hope this helps,
Ed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900