3 Replies - 3849 Views - Last Post: 14 February 2010 - 08:54 AM Rate Topic: -----

#1 Guest_jaikob*


Reputation:

Drawing on a picture box getting reset

Posted 13 February 2010 - 10:21 PM

Whenever I call MessageBox.Show("ttt"); my picturebox is cleared of all of its contents. What I am trying to achieve is to create a paint like replica where I am able to draw on the picture box. My code is below:

private void canvasDown(object sender, MouseEventArgs e)
        {
            draw = true;
            firstX = e.X;
            firstY = e.Y;
            OldX = e.X;
            OldY = e.Y;
        }

        private void canvasUp(object sender, MouseEventArgs e)
        {
            draw = false;
        }

        private void canvasMove(object sender, MouseEventArgs e)
        {
            if (draw == true)
            {
                Graphics g = canvas.CreateGraphics();
                g.SmoothingMode = SmoothingMode.AntiAlias;
                // Set the correct params
                if (color == "black")
                {
                    Pen myPen = new Pen(Color.Black, brushSize);
                    g.DrawLine(myPen, OldX, OldY, e.X, e.Y);
                    OldX = e.X;
                    OldY = e.Y;
                } else if (color == "red") {
                    Pen myPen = new Pen(Color.Red, brushSize);
                    g.DrawLine(myPen, OldX, OldY, e.X, e.Y);
                    OldX = e.X;
                    OldY = e.Y;
                }
                else if (color == "green")
                {
                    Pen myPen = new Pen(Color.Green, brushSize);
                    g.DrawLine(myPen, OldX, OldY, e.X, e.Y);
                    OldX = e.X;
                    OldY = e.Y;
                } else if (color == "blue") {
                    Pen myPen = new Pen(Color.Blue, brushSize);
                    g.DrawLine(myPen, OldX, OldY, e.X, e.Y);
                    OldX = e.X;
                    OldY = e.Y;
                }
                else if (color == "yellow")
                {
                    Pen myPen = new Pen(Color.Yellow, brushSize);
                    g.DrawLine(myPen, OldX, OldY, e.X, e.Y);
                    OldX = e.X;
                    OldY = e.Y;
                }
                else if (color == "magenta")
                {
                    Pen myPen = new Pen(Color.Magenta, brushSize);
                    g.DrawLine(myPen, OldX, OldY, e.X, e.Y);
                    OldX = e.X;
                    OldY = e.Y;
                }
                else if (color == "cyan")
                {
                    Pen myPen = new Pen(Color.Cyan, brushSize);
                    g.DrawLine(myPen, OldX, OldY, e.X, e.Y);
                    OldX = e.X;
                    OldY = e.Y;
                }
                else
                {
                    Pen myPen = new Pen(Color.Black, 1);
                    g.DrawLine(myPen, OldX, OldY, e.X, e.Y);
                    OldX = e.X;
                    OldY = e.Y;
                }
 }


        }


color and brushSize are public variables. When I click a button to display a messagebox, the picture box is erased. What am I doing wrong? It's fine without any messagebox.

Thanks.

Is This A Good Question/Topic? 0

Replies To: Drawing on a picture box getting reset

#2 Guest_jaikob*


Reputation:

Re: Drawing on a picture box getting reset

Posted 13 February 2010 - 10:28 PM

And for some reason I couldn't login -_-
Was This Post Helpful? 0

#3 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Drawing on a picture box getting reset

Posted 14 February 2010 - 05:21 AM

Hi.
The fact that a MessageBox is displayed causes a redraw of your form.
This means that the PictureBox is cleared and redrawn by the framework.
The framework doesn't know about your drawing, and can't do that for you.
So what you should do is not just draw to the picturebox-area, but also either:

keep track of all drawing coordinates and colours and add a handler for the Paint() event of the picturebox.
When that handler is called, you should then redraw all lines that were drawn before.

or:

Create a bitmap for the picturebox(canvas) and do all drawing there, adding code similar to:
// in your class, add a bitmap variable:
        Bitmap drawBitmap; 

// in your ....Form_Load() add:
        drawBitmap = new Bitmap(canvas.Width,canvas.Height); // create a bitmap with the same size as the canvas;



Then change your drawing method so it draws to that bitmap instead of 'canvas':

                Graphics g = Graphics.FromImage(drawBitmap);



When you do this, the drawing only happens in the 'invisible' bitmap.
To make sure that it gets drawn to the visible control as well, add an 'Invalidate' call to the end of your drawing method:

.
.
.
                else
                {
                    Pen myPen = new Pen(Color.Black, 1);
                    g.DrawLine(myPen, OldX, OldY, e.X, e.Y);
                    OldX = e.X;
                    OldY = e.Y;
                }
       // make the system repaint canvas, thus showing the line that was just added:
       canvas.Invalidate();
       // nicely clean up our graphics object
       g.Dispose();

 } // closing brace of draw == true


}




Good Luck!
Was This Post Helpful? 1
  • +
  • -

#4 jaikob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 05-January 10

Re: Drawing on a picture box getting reset

Posted 14 February 2010 - 08:54 AM

Thank you. That fixed it :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1