创建新图片框的问题

本文关键字:问题 新图片 创建 | 更新日期: 2023-09-27 17:50:17

首先我很抱歉我的英语不好。现在我有问题与我的c#项目(ms油漆)。当我在图片框中打开新图片时,我画的最后一个形状仍然保留,直到我在这个图像上画另一条线。下面是我的代码:
-划线:

 public Form1()
    {
        InitializeComponent();
        snapshot = new Bitmap(pictureBox1.Width, pictureBox1.Height);
     }
 if (tempDraw != null)
                {
                    tempDraw = (Bitmap)snapshot.Clone();
                    Graphics g = Graphics.FromImage(tempDraw);
                    Pen myPen = new Pen(colorPickerDropDown1.SelectedColor, 5);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                    g.DrawLine(myPen, pDau, pHientai);
                    myPen.Dispose();
                    e.Graphics.DrawImageUnscaled(tempDraw, 0, 0);
                    g.Dispose();
                }

-鼠标事件:

 private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        paint = false;
        snapshot = (Bitmap)tempDraw.Clone();
    }
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        paint = true;
        saved = false;
        pDau = e.Location;
        tempDraw = (Bitmap)snapshot.Clone();
    }
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (paint)
        {
            pHientai = e.Location;
            pictureBox1.Invalidate();
            saved = false;
        }
    }

-创建新的图片框:

   public void New()
    {
        pictureBox1.Image =null;
        snapshot = null;
        tempDraw = null;
        snapshot= new Bitmap(pictureBox1.Width, pictureBox1.Height);
    }

-打开图片:

  New();
                snapshot = new Bitmap(openFileDialog1.FileName);
                tempDraw = (Bitmap)snapshot.Clone();
                pictureBox1.Image = new Bitmap(openFileDialog1.FileName);
                strPath = openFileDialog1.FileName;
                this.Text = strPath + " - Paint";

你能告诉我哪里不对吗?非常感谢!

创建新图片框的问题

在您的第一个代码示例中,我假设整个if语句实际上在表单的Paint事件中是正确的吗?这样的:

private void Form_Paint(object sender, PaintEventArgs e)
{
    if (tempDraw != null)
    {
        tempDraw = (Bitmap)snapshot.Clone();
        Graphics g = Graphics.FromImage(tempDraw);
        Pen myPen = new Pen(colorPickerDropDown1.SelectedColor, 5);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
        g.DrawLine(myPen, pDau, pHientai);
        myPen.Dispose();
        e.Graphics.DrawImageUnscaled(tempDraw, 0, 0);
        g.Dispose();
    }
}

如果是,考虑调用e.Graphics.Clear(this.BackColor)来清除窗体与自己的背景色。这将有效地擦掉你画的任何东西。另外,在创建绘图对象时考虑using语句,以便在任何方法抛出异常时保护您。我会重写你的if语句,像这样:

    if (tempDraw != null)
    {
        tempDraw = (Bitmap)snapshot.Clone();
        using (Graphics g = Graphics.FromImage(tempDraw))
        using (Pen myPen = new Pen(colorPickerDropDown1.SelectedColor, 5))
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            g.DrawLine(myPen, pDau, pHientai);
            e.Graphics.Clear(this.BackColor); // clear any drawing on the form
            e.Graphics.DrawImageUnscaled(tempDraw, 0, 0);
        }
    }