Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

c# - how to draw drawings in picture box

draw pictures in picture Box on Mouse dragging using c#

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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):

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;
}

And then draw away!

If you like, you can improve the quality of the drawn image somewhat by setting the Graphics object's SmoothingMode property.

Update: .Net CF does't have the Pens collection, and MoustEventArgs doesn't have a Location, so here is a CF-friendly version:

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...