Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear friends,


i have this code for drawlines in picturebox and i want that all lines are remove in button click , and after user mouse click then these lines are not show.

thanks
C#
int iNumberofClicks = 0;
        Point[] pointArray = new Point[100];

 private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            pointArray[iNumberofClicks].X = e.X;
            pointArray[iNumberofClicks].Y = e.Y;
            Pen PenSpikes = new Pen(Color.Green);
            SolidBrush solidBrush = new SolidBrush(Color.Blue);

            if(iNumberofClicks==0)
            {
                 a = e.X;
                 b = e.Y;
            }

            iNumberofClicks++;
            if (iNumberofClicks > 1)
            {
                Point[] CurrentpointArray = new Point[iNumberofClicks];

                for (int aa = 0; aa < iNumberofClicks; aa++)
                {
                    CurrentpointArray[aa].X = pointArray[aa].X;
                    CurrentpointArray[aa].Y = pointArray[aa].Y;

                }

                Graphics offScreenDC = Graphics.FromImage(pictureBox1.Image);
                offScreenDC.DrawLines(PenSpikes, CurrentpointArray);
                offScreenDC.Dispose();
                pictureBox1.Refresh();
                //
                for (int j = 1; j < CurrentpointArray.Length;j++ )
                {
                    //int d=(Convert.ToInt16(CurrentpointArray[j].X));
                    //int f=Convert.ToInt16(CurrentpointArray[j].Y);
                    if (Convert.ToInt16(CurrentpointArray[j].X) == a  && Convert.ToInt16(CurrentpointArray[j].Y) == b  )
                    {
                        MessageBox.Show("alert");
                            break;
                    }
                }
                Array.Clear(CurrentpointArray,0,CurrentpointArray.Length);
            }
        }
Posted
Updated 18-May-12 21:09pm
v2

1 solution

The problem is that you are drawing directly on the image:
C#
Graphics offScreenDC = Graphics.FromImage(pictureBox1.Image);
Which means that the Image held in the PictureBox control is modified directly. The only way to "undo" those changes is to reload the image from the original source.

BTW: If you construct a Graphics object as above, then you are responsible for calling Dispose on it - if you don't, then your program will crash unpredictably, as Graphics contexts are scarce resource.

Personally, I would handle the PictureBox Paint event, and draw all the required lines there using the Graphics context supplied, rather than drawing on the Image directly (unless you intend to save the Image afterwards, and even then I would just apply the changes immediately before I wrote the file).
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900