using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace crop2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//The lines that have been drawn but not saved.
private List<Line> lines = new List<Line>();
//The start point of the line currently being drawn.
private Point start;
private void Form1_Load(object sender, System.EventArgs e)
{
//Place a blank image in the PictureBox control.
this.pictureBox1.Image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
}
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//Remember the point at which the line started.
this.start = e.Location;
}
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
//Remember the point at which the line ended.
Point end = e.Location;
//Add the new line to the list.
this.lines.Add(new Line(this.start, end));
//Force the control to repaint so the new line is drawn.
this.pictureBox1.Invalidate(new Rectangle(Math.Min(this.start.X, end.X), Math.Min(this.start.Y, end.Y), Math.Abs(this.start.X - end.X), Math.Abs(this.start.Y - end.Y)));
this.pictureBox1.Update();
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//Draw each line on the control.
foreach (Line line in this.lines)
{
e.Graphics.DrawLine(Pens.Black, line.Start, line.End);
}
}
private void Save()
{
//Create a Graphics object from the Image in the PictureBox.
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
//Draw each line on the image to make them permanent.
foreach (Line line in this.lines)
{
g.DrawLine(Pens.Black, line.Start, line.End);
}
//Clear the temporary lines that were just saved.
this.Clear();
}
private void Clear()
{
//Clear all unsaved lines.
this.lines.Clear();
//Force the control to repaint so the lines are removed.
this.pictureBox1.Refresh();
}
}
public class Line
{
//The line's start point.
private Point _start;
//The line's end point.
private Point _end;
//The line's start point.
public Point Start
{
get { return this._start; }
set { this._start = value; }
}
//The line's end point.
public Point End
{
get { return this._end; }
set { this._end = value; }
}
public Line():this(Point.Empty, Point.Empty)
{
}
public Line(Point start, Point end)
{
this._start = start;
this._end = end;
}
}
}