1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public partial class Form1 : Form { int xFirst, yFirst; Bitmap bm = new Bitmap(1000, 1000); Graphics bmG; Pen pen = new Pen(Color.Black, 1); bool draw = false;
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { bmG = Graphics.FromImage(bm); bmG.Clear(Color.White); }
private void Form1_MouseDown(object sender, MouseEventArgs e) { xFirst = e.X; yFirst = e.Y; draw = true; }
private void Form1_MouseUp(object sender, MouseEventArgs e) { bmG.DrawLine(pen, xFirst, yFirst, e.X, e.Y); draw = false; Invalidate(); }
private void Form1_MouseMove(object sender, MouseEventArgs e) { if (draw) { Invalidate(); } }
private void Form1_Paint(object sender, PaintEventArgs e) { if (draw) { e.Graphics.DrawImage(bm, 0, 0); e.Graphics.DrawLine(pen, xFirst, yFirst, e.X, e.Y); } else { e.Graphics.DrawImage(bm, 0, 0); } } }
|