Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Put a PictureBox on your form, and set its BackColor to White. Then add this code to your form (you have to actually hook up the Mouse events below, i.e. you can't just copy and paste this code into your form):</p> <pre><code>private Point? _Previous = null; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { _Previous = e.Location; pictureBox1_MouseMove(sender, e); } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (_Previous != null) { if (pictureBox1.Image == null) { Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.White); } pictureBox1.Image = bmp; } using (Graphics g = Graphics.FromImage(pictureBox1.Image)) { g.DrawLine(Pens.Black, _Previous.Value, e.Location); } pictureBox1.Invalidate(); _Previous = e.Location; } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { _Previous = null; } </code></pre> <p>And then draw away!</p> <p>If you like, you can improve the quality of the drawn image somewhat by setting the <code>Graphics</code> object's <code>SmoothingMode</code> property.</p> <p><strong>Update:</strong> .Net CF does't have the <code>Pens</code> collection, and <code>MoustEventArgs</code> doesn't have a <code>Location</code>, so here is a CF-friendly version:</p> <pre><code>private Point? _Previous = null; private Pen _Pen = new Pen(Color.Black); private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { _Previous = new Point(e.X, e.Y); pictureBox1_MouseMove(sender, e); } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (_Previous != null) { if (pictureBox1.Image == null) { Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.White); } pictureBox1.Image = bmp; } using (Graphics g = Graphics.FromImage(pictureBox1.Image)) { g.DrawLine(_Pen, _Previous.Value.X, _Previous.Value.Y, e.X, e.Y); } pictureBox1.Invalidate(); _Previous = new Point(e.X, e.Y); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { _Previous = null; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload