Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建新图片框时出现问题!_C#_Image_Bitmap_Gdi+ - Fatal编程技术网

C# 创建新图片框时出现问题!

C# 创建新图片框时出现问题!,c#,image,bitmap,gdi+,C#,Image,Bitmap,Gdi+,首先,我很抱歉我的英语不好。现在我的C#项目(ms paint)遇到了问题。当我在图片框中打开新图片时,我绘制的最后一个形状仍然保留,直到我在此图像上绘制另一条线。这是我的代码: -划线: public Form1() { InitializeComponent(); snapshot = new Bitmap(pictureBox1.Width, pictureBox1.Height); } if (tempDraw != null)

首先,我很抱歉我的英语不好。现在我的C#项目(ms paint)遇到了问题。当我在图片框中打开新图片时,我绘制的最后一个形状仍然保留,直到我在此图像上绘制另一条线。这是我的代码:
-划线:

 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.Case.清除(这个.BuffCor)< /C> >以其自己的背景颜色清除窗体。这将有效地删除您绘制的任何内容。此外,如果创建任何一个方法抛出异常,请考虑使用< <代码>使用“<代码>语句”来保护您。我会重写你的

如果
声明如下:

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

首先,感谢您的回复。但这个“如果”的说法实际上是在picturebox_paint事件中出现的。我在Form1中添加了一个图片框,并在上面绘制。然后,您的代码只需将我的picturebox颜色更改为与表单背景相同的颜色。我的问题是,当我在picturebox上画了一些东西后,我从电脑上打开了新的图像,我画的最后一个形状没有消失,直到我在picturebox上画了其他东西,它才消失。
picturebox
不是用来画画的。它用于显示图像,通常来自文件。直接在
表格
面板
上绘制。