这不是很难。在 Winforms 中,这是一个最小的示例:
GraphicsPath GP = null;
List<Point> points = new List<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
points.Clear();
points.Add(e.Location);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
GP = new GraphicsPath();
GP.AddClosedCurve(points.ToArray());
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
points.Add(e.Location);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (points.Count > 1)
e.Graphics.DrawCurve(Pens.Orange, points.ToArray(), 0.5f);
}
private void cb_clearRegion_Click(object sender, EventArgs e)
{
points.Clear();
pictureBox1.Region = null;
}
private void cb_SaveRegion_Click(object sender, EventArgs e)
{
Rectangle cr = pictureBox1.ClientRectangle;
using (Bitmap bmp = new Bitmap(cr.Width, cr.Height))
using (Graphics G = Graphics.FromImage(bmp))
{
G.SetClip(GP);
G.DrawImage(pictureBox1.Image, Point.Empty);
bmp.Save(@"D:\xyz.png", ImageFormat.Png);
}
}
请注意,这不使用放大或缩小,而是创建与原始大小相同的位图,在区域外的任何地方都是透明的。
使用ScaleTransform 和Point UnZoom(Point) 函数实现azoom 很简单;只是问你是否需要它..
如果要添加“移动”模式,可以使用MouseMove 并重新计算所有Points。
如果您想要多个区域,则必须收集 List<T> 并连续使用它们来创建输出。
如果您真的只想保存网格而不保存图像,请使用 G.DrawPath(..) 而不是 DrawImage() !
还请注意,您可能需要使用各种绘图工具(如线条、矩形等)来优化选择。您可以将图形逐步添加到路径中..