namespace WindowsFormsApplication2
{
public partial class Form4 : Form
{
Pen p = null;
Graphics g = null;
List<int> x = new List<int>();
List<int> y = new List<int>();
SolidBrush b = new SolidBrush(Color.Red);
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openfileDialog = new OpenFileDialog();
//openfileDialog.Filter = "GIF图片(*.gif)|*.gif|JPG图片(*.jpg)|*.jpg|BMP图片(*.bmp)|*.bmp|PNG图片(*.png)|*.png|所有图片文件(*.gif,*.jpg,*.bmp,*.png)|*.gif;*.jpg;*.bmp;*.png";
if ((openfileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK))
{
this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
this.pictureBox1.ImageLocation = openfileDialog.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
g.FillRectangle(b, e.X, e.Y, 2, 2);
if (x.Count > 0 || y.Count > 0)
{
x.Clear();
y.Clear();
}
x.Add(e.X);
y.Add(e.Y);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
g.FillRectangle(b,e.X,e.Y,2,2);
x.Add(e.X);
y.Add(e.Y);
if (x.Count > 1 || y.Count > 1)
{
g.DrawLine(p,x[x.Count-2],y[y.Count-2],x[x.Count-1],y[y.Count-1]);
}
}
}
private void Form4_Load(object sender, EventArgs e)
{
if (p == null)
{
p = new Pen(Color.Red, 0.5f);
}
if(g==null)
{
g=this.pictureBox1.CreateGraphics();
}
}
}
}