当前位置: 代码迷 >> C# >> picturebox画图, 已经实现,求怎么保存图片
  详细解决方案

picturebox画图, 已经实现,求怎么保存图片

热度:77   发布时间:2016-05-05 04:50:56.0
picturebox画图, 已经实现,求如何保存图片

 private Point P3, P4;//定义两个点(启点,终点)  
 private static bool drawing = false;//设置一个启动标志
  private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            P3 = new Point(e.X, e.Y);
            P4 = new Point(e.X, e.Y);
            drawing = true;  
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g = pictureBox1.CreateGraphics();
            //Graphics g = Graphics.FromImage(this.pictureBox1.Image);
            if (e.Button == MouseButtons.Left)
            {
                if (drawing)
                {
                    //drawing = true;  
                    Point currentPoint = new Point(e.X, e.Y);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿  
                    g.DrawLine(new Pen(Color.Blue, 2), P4, currentPoint);

                    P4.X = currentPoint.X;
                    P4.Y = currentPoint.Y;
                }
            }  

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            drawing = false;  
        }
      private void button5_Click(object sender, EventArgs e)
        {
            pictureBox1.Image.Save(@"D:\2.jpg", ImageFormat.Jpeg);
            MessageBox.Show("ok");
        }




如上已经实现可以再picturebox上画图,但是点击btn5想保存下来图片,是不是不应该这么操作,而是要到paint里面去操作,但是paint里面我不知道如何实现实时的鼠标移动画线,类似标注的功能,现在是可以满足需求的。就是想把标注的内容随原来的图片一起保存下来。
------解决思路----------------------
那你就在按钮里重新执行
Graphics g = pictureBox1.CreateGraphics();
并把它画到一个新建Image里,然后save
------解决思路----------------------
Graphics g = pictureBox1.CreateGraphics();
//Graphics g = Graphics.FromImage(this.pictureBox1.Image);

被你注释掉的那句不就可以么,不要创建 Graphics
另外 Graphics 的 Dispose 不要忘了
------解决思路----------------------
不要在MouseMove里面CreateGraphics,它会导致GDI资源泄露,一定时间后可能会导致程序崩溃等。
用this.pictureBox1.Invalidate来强迫它更新。
用数据结构(那些currentScribble ,scribbles)等来保存自定义标志,并在更新,或存图的时候重画。
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.pictureBox1.Paint += pictureBox1_Paint;  // 可以移动到设计器生成代码里
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        currentScribble = new List<Point>();
        currentScribble.Add(e.Location);
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && this.currentScribble != null)
        {
            currentScribble.Add(e.Location);
            RedrawPictureBox();
        }
    }

    List<Point> currentScribble = null;
    List<Point[]> scribbles = new List<Point[]>();
    void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        using (Pen pen = new Pen(Color.Blue, 2))
        {
            foreach (Point[] points in scribbles)
            {
                e.Graphics.DrawLines(pen, points);
            }
            if (this.currentScribble != null)
            {
                e.Graphics.DrawLines(pen, this.currentScribble.ToArray());
            }
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (currentScribble != null && currentScribble.Count > 1)
        {
            RedrawPictureBox();
            this.scribbles.Add(currentScribble.ToArray());
        }
        this.currentScribble = null;
    }

    private void RedrawPictureBox()
    {
        if (currentScribble.Count >= 2)
        {
            Rectangle last = new Rectangle(currentScribble[currentScribble.Count - 2], Size.Empty);
            Rectangle current = new Rectangle(currentScribble[currentScribble.Count - 1], Size.Empty);
            Rectangle invalidateArea = Rectangle.Inflate(Rectangle.Union(last, current), 2, 2);
            this.pictureBox1.Invalidate(invalidateArea);
        }
    }

    private void button5_Click(object sender, EventArgs e)
    {
        Rectangle rect = new Rectangle(Point.Empty, this.pictureBox1.Size);
        using (Bitmap bmp = new Bitmap(rect.Width, rect.Height))
        {
            this.pictureBox1.DrawToBitmap(bmp, rect);  // 画pictureBox1显示的图,假定它没有边框
            using (Graphics g = Graphics.FromImage(bmp))
            {
                pictureBox1_Paint(this, new PaintEventArgs(g, rect)); // 画自定义标记
            }
            bmp.Save(@"D:\2.jpg", ImageFormat.Jpeg);
        }
        MessageBox.Show("ok");
    }
}