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

I am working on drawing Lines using (DrawLines) over Image in picturebox,I am able to draw lines using below code,,But i want when user click on Button (provided below picture box) all the lines which user have drawn over the image should be Erased .

Thanks,,,,,



C#
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++ )
              {
                  if ((Convert.ToInt16(CurrentpointArray[j].X) == a && Convert.ToInt16(CurrentpointArray[j].Y) == b) )
                  {
                      MessageBox.Show("alert");
                      break;
                  }
              }
Posted

1 solution

Before you draw the first line you need to cache the original image in memory. Something like this:

C#
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms)


Then when you want to clear the lines you reload the original image. Again something like this:

C#
Image img = new Image(ms);
pictureBox1.Image = img;
 
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