开发者

Problem with creating a new picture box!

开发者 https://www.devze.com 2023-02-25 01:58 出处:网络
First I\'m sorry about my bad English. Now I\'m having the problem with my C# project ( ms paint). When I open new picture in picture box, the last shape i drew still remain, until I draw the other li

First I'm sorry about my bad English. Now I'm having the problem with my C# project ( ms paint). When I open new picture in picture box, the last shape i drew still remain, until I draw the other line on this image. Here is my code:

-Draw line:

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

- mouse events:

 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开发者_如何学JAVA();
    }
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (paint)
        {
            pHientai = e.Location;
            pictureBox1.Invalidate();
            saved = false;
        }
    }

- create new picture box:

   public void New()
    {

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

-Open the image:

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

can you tell me something wrong? Thnks you so much !


In your very first code sample, am I correct in assuming that the entire if statement is actually within the form's Paint event? Like this:

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

If so, consider calling e.Graphics.Clear(this.BackColor) to clear the form with its own background color. This will effectively erase anything you have drawn. Also, consider the using statement when creating drawing objects to protect you if any method throws an exception. I'd rewrite your if statement like so:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号