我是图像处理的新手。我想知道如何在C#windows窗体应用程序中进行Free hand Image Cropping? 首先,我想在图像中绘制对象的边界,然后根据绘制的边界裁剪对象。 这样做的方法是什么?
谢谢!
答案 0 :(得分:2)
您可以在控件的X
个事件的Y
和MouseDown
以及Right
上获取裁剪区域的Bottom
和MouseUp
显示你的图像,你将拥有裁剪区域坐标。
最后像这样调整图像大小:
var cropedImage = yourImage.Clone(new Rectangle(x, y, width, height), yourImage.PixelFormat);
答案 1 :(得分:0)
根据定义,图像是矩形的,因此即使您“裁剪非矩形区域”,结果仍然是矩形图像。试试这个:
答案 2 :(得分:0)
我制作了裁剪图像的代码。我们只需要两个图片框。一个用于原始图像,另一个用于裁剪图像。然后看下面的代码。您必须在事件处理程序部分写下这些代码。
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);
}
有效。尝试并享受免费编码。