Click here to Skip to main content
11,409,138 members (64,380 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Dear Friends,

I have created mutiple ellipse over a picture box and stored points, Now I am recreating all ellipse on a Button click based on stored points. But my recreate function is not working fine.It is not showing any ellipse.

Please Help me out ,

My code is here:


private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
         //   Point mouseDownLocation = pictureBox1.PointToClient(new Point(e.X, e.Y));
            mRect = new Rectangle(e.X, e.Y, 0, 0);
 
        }
 
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
            }
        }
 
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Color c1 = Color.FromArgb(50, Color.Green);
 
            string ss = mRect.X.ToString() + "," + mRect.Y.ToString() + "," + mRect.Width.ToString() + "," + mRect.Height.ToString();
            //Draw a rectangle with 2pixel wide line
            using (Pen pen = new Pen(Color.Red, 2))
            {
                Graphics myGraphics = pictureBox1.CreateGraphics();
                myGraphics.DrawEllipse(pen, mRect);
                myGraphics.FillEllipse(new SolidBrush(c1), mRect);
                listBox1.Items.Add(ss);
                
            }
        }
 
private void btnSaveObject_Click(object sender, EventArgs e)
        {
           
            recreate();
        }
        private void recreate()
        {
            
            pictureBox1.Invalidate();
            foreach (string str in listBox1.Items)
            {
                string[] strArr = str.Split(new Char[] { ',' });
                using (Pen pen = new Pen(Color.Red, 2))
                {
                    Graphics myGraphics = pictureBox1.CreateGraphics();
                    myGraphics.DrawEllipse(pen, Convert.ToInt32(strArr[0]), Convert.ToInt32(strArr[1]), Convert.ToInt32(strArr[2]), Convert.ToInt32(strArr[3]));
                   
                    myGraphics.Dispose();
                    pictureBox1.Refresh();
                }
            }
        }
Posted 22-Nov-12 2:18am
Edited 22-Nov-12 2:19am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Remove the line:
                    pictureBox1.Refresh();
It causes the PictureBox to redraw from scratch, throwing away the ellipses you just drew...

Better, don't draw any of the ellipses in the mouse or button handlers - handle the PictureBox.Paint event and do them there. Use PictureBox.Invalidate() to cause it to redraw them - if you don't then they will disappear if you minimise the form, or put another form over the top
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Actually, the ellipses get drawn, but immediately erased by your last statement: pictureBox1.Refresh();.

The correct way, however is not to draw at all in the MouseUp event. MouseUp should confine itself to storing the necessary data and calling pictureBox1.Invalidate();.

The drawing then will happen in pictureBox1_Paint. Subscribe that method to pictureBox1's Paint event. In that method, do as you tried in your Recreate() method but without Refresh and Invalidate.

The Paint method will have a PaintEventArgs as second parameter. Use the PaintEventArgs.Graphics object rather than creating a new one.

For later:
1. You don't need a PictureBox to draw on. That can be achieved with any Control-derived class. Use a PictureBox if you need its ability to show existing pictures.

2. Storing ellipse co-ordinates in a string is not the way to go, use a List<Rectangle>[^] instead.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 310
2 Maciej Los 155
3 Peter Leow 130
4 Dave Kreskowiak 115
0 Sergey Alexandrovich Kryukov 8,676
1 OriginalGriff 6,855
2 Maciej Los 3,322
3 Abhinav S 3,238
4 Peter Leow 3,034


Advertise | Privacy | Mobile
Web03 | 2.8.150414.5 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100