我是图像处理的新手。我想知道如何在C# windows窗体应用程序中进行徒手图像裁剪?首先,我想要在图像中绘制对象的边界,然后根据绘制的边界对对象进行裁剪。有什么方法可以做到这一点呢?
谢谢!
发布于 2011-11-24 21:09:15
您可以在MouseDown上获取裁剪区域的X和Y,在显示图像的控件的MouseUp事件上获取Right和Bottom,这样就可以获得裁剪区域坐标。
最后,像这样调整图像的大小:
var cropedImage = yourImage.Clone(new Rectangle(x, y, width, height), yourImage.PixelFormat);发布于 2011-11-25 14:42:46
根据定义,图像是矩形的,因此即使您“裁剪非矩形区域”,结果仍将是矩形图像。试试这个:
发布于 2016-06-13 21:23:17
我已经编写了裁剪图像的代码。简单地说,我们需要两个图片盒。一个用于原始图像,另一个用于裁剪图像。然后看下面的代码。这些代码必须写在事件处理程序部分。
int cropX, cropY, cropWidth, cropHeight;
//here rectangle border pen color=red and size=2;
Pen borderpen = new Pen(Color.Red, 2);
System.Drawing.Image _orgImage;
Bitmap crop;
//fill the rectangle color =white
SolidBrush rectbrush = new SolidBrush(Color.FromArgb(100, Color.White));
private void pic_scan_MouseDown(object sender, MouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Left)//here i have use mouse click left button only
{
pic_scan.Refresh();
cropX = e.X;
cropY = e.Y;
Cursor = Cursors.Cross;
}
pic_scan.Refresh();
}
catch { }
}
private void pic_scan_MouseMove(object sender, MouseEventArgs e)
{
try
{
if (pic_scan.Image == null)
return;
if (e.Button == MouseButtons.Left)//here i have use mouse click left button only
{
pic_scan.Refresh();
cropWidth = e.X - cropX;
cropHeight = e.Y - cropY;
}
pic_scan.Refresh();
}
catch { }
}
private void pic_scan_MouseUp(object sender, MouseEventArgs e)
{
try
{
Cursor = Cursors.Default;
if (cropWidth < 1)
{
return;
}
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(cropX, cropY, cropWidth, cropHeight);
Bitmap bit = new Bitmap(pic_scan.Image, pic_scan.Width, pic_scan.Height);
crop = new Bitmap(cropWidth, cropHeight);
Graphics gfx = Graphics.FromImage(crop);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;//here add System.Drawing.Drawing2D namespace;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;//here add System.Drawing.Drawing2D namespace;
gfx.CompositingQuality = CompositingQuality.HighQuality;//here add System.Drawing.Drawing2D namespace;
gfx.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel);
}
catch { }
}
private void pic_scan_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(cropX, cropY, cropWidth, cropHeight);
Graphics gfx = e.Graphics;
gfx.DrawRectangle(borderpen, rect);
gfx.FillRectangle(rectbrush, rect);
}它起作用了。试试吧,享受它的免费编码。
https://stackoverflow.com/questions/8257497
复制相似问题