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);
}
else
{
objCurrImageHandler.CurrentCropHandler.RemoveCropAreaDraw();
}
}
}
catch (Exception ex)
{
}
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?