如何绘制图片框图纸 [英] how to draw drawings in picture box

查看:132
本文介绍了如何绘制图片框图纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绘制图片框的照片上使用C#鼠标拖动

draw pictures in picture Box on Mouse dragging using c#

推荐答案

把你的窗体上的图片框,它的背景色设置为白色。那么这个code添加到您的表单(你必须真正挂钩下面的鼠标事件,即你不能只是复制并code粘贴到您的形式):

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

和再画了!

如果你愿意,你可以在一定程度通过设置图形对象的 Smoothi​​ngMode 属性提高了绘制图像的质量

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

更新:的.Net CF简化版,有集合和 MoustEventArgs 不具有位置,所以这里是一个CF的版本:

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

这篇关于如何绘制图片框图纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆