110,533
社区成员
发帖
与我相关
我的任务
分享
public partial class Form1 : Form
{
public Form1()
{
//InitializeComponent();
this.Text = "鼠标左键点击选点,右键结束区域选择";
}
Bitmap bmp1 = new Bitmap(@"c:\temp\penguins.jpg");//要换成你的图片
Bitmap bmp2 = new Bitmap(@"c:\temp\Tulips.jpg"); //要换成你的图片
Region region = null;
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(bmp1, this.ClientRectangle);
if (region != null)
{
e.Graphics.Clip = region;
e.Graphics.DrawImage(bmp2, this.ClientRectangle);
}
}
List<Point> points = new List<Point>();
protected override void OnMouseClick(MouseEventArgs e)
{
points.Add(e.Location);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
using(Graphics g = this.CreateGraphics())
{
Rectangle rect = Rectangle.Inflate(new Rectangle(points[0], Size.Empty), 2,2);
g.FillEllipse(Brushes.Red, rect);
if (points.Count >= 2)
{
g.DrawLines(Pens.Red, points.ToArray());
}
}
}
else if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
this.region = null;
if (points.Count >= 3)
{
using (GraphicsPath path = new GraphicsPath())
{
path.StartFigure();
path.AddLines(this.points.ToArray());
path.CloseFigure();
this.region = new Region(path);
}
}
this.points.Clear();
this.Invalidate();
}
}
}
public partial class Form1 : Form
{
Outline _outline;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (_outline == null)
{
_outline = new Outline();
_outline.Closed += _outline_Closed;
}
else if (_outline.IsClosed)
return;
_outline.AddPoint(e.Location);
pictureBox1.Invalidate();
}
else if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
if (_outline != null)
_outline.Close();
}
}
void _outline_Closed(object sender, EventArgs e)
{
Outline outline = (Outline)sender;
GraphicsPath path = new GraphicsPath();
path.AddPolygon(outline.Points);
pictureBox1.Region = new System.Drawing.Region(path);
pictureBox1.Invalidate();
path.Dispose();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (_outline != null)
{
Graphics g = e.Graphics;
Point[] points = _outline.Points;
if (!_outline.IsClosed)
{
if (points.Length > 0)
{
Rectangle clip = e.ClipRectangle;
foreach (Point pt in points)
{
if (clip.Contains(pt))
{
Rectangle rect = new Rectangle(pt.X - 2, pt.Y - 2, 5, 5);
g.DrawRectangle(Pens.Red, rect);
}
}
if (points.Length > 1)
{
g.DrawLines(Pens.Red, points);
}
}
}
else if (pictureBox1.Image == null)
{
g.FillPolygon(Brushes.Red, points);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
_outline = null;
pictureBox1.Region = null;
pictureBox1.Invalidate();
}
class Outline
{
public event EventHandler Closed;
List<Point> _points;
bool _closed;
public Outline()
{
_points = new List<Point>(10);
}
public Point[] Points
{
get { return _points.ToArray(); }
}
public void AddPoint(Point point)
{
if (_closed)
throw new InvalidOperationException();
if (_points.Count > 0)
{
Point init = _points[0];
Rectangle test = new Rectangle(init.X - 2, init.Y - 2, 5, 5);
if (test.Contains(point))
{
Close();
return;
}
}
_points.Add(point);
}
public void AddPoint(int x, int y)
{
AddPoint(new Point(x, y));
}
public void Close()
{
if (!_closed)
{
_closed = true;
if (Closed != null)
Closed(this, EventArgs.Empty);
}
}
public bool IsClosed
{
get { return _closed; }
}
}
}