在C#中裁剪显示异常 [英] Cropping in c# shows Out of exceptions
本文介绍了在C#中裁剪显示异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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?
推荐答案
这看起来像是一种复杂的裁剪图像的可怕方法,特别是在我找到了示例的情况下.请查看下一页上的部分,其中包含用于裁剪图像的漂亮的简短代码段:
http://www.switchonthecode.com/tutorials/csharp-tutorial-图像编辑节省裁切和调整大小 [ ^ ]
具体来说,这段代码片段:(摘自上面的原始资源)
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)
private static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (Image)(bmpCrop);
}
希望这会有所帮助,
Ed
Hope this helps,
Ed
这篇关于在C#中裁剪显示异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文