2013-12-09 39 views
0

我有一個pictureBox1與圖像裏面,當我點擊它的繪圖點。 現在我添加了一個重新設置按鈕,當我點擊它時,它應該清除我在pictureBox上所做的所有圖紙,並將圖像留在裏面而沒有圖紙。如何重置/清除我在pictureBox1上做的所有圖紙?

我所做的:

private void button4_Click(object sender, EventArgs e) 
     { 
      Graphics graphics; 
      graphics = pictureBox1.CreateGraphics(); 
      graphics.DrawImage(pictureBox1.Image, 0, 0); 
     } 

所以我畫了很多pictureBox1點,然後按一下按鈕,所有的點都走了,但再一次,我又在picturebox1點擊我也看到了新的點,但也我在清理前做過的幾點。

我該如何清除舊圖紙,以便在下次點擊時不會出現?

這是Paint事件:移動的油漆事件到一個新的類:

public static void Paint(List<PointF> pb1points, GraphicsPath pb1gp, Point movingPoint, PictureBox pictureBox1, Graphics e) 
     { 
      e.Clear(Color.White); 
      e.DrawImage(pictureBox1.Image, movingPoint); 
      Pen p; 
      p = new Pen(Brushes.Green); 
      foreach (PointF pt in pb1points) 
      { 
       e.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f); 
      } 
      using (Pen pp = new Pen(Color.Green, 2f)) 
      { 
       pp.StartCap = pp.EndCap = LineCap.Round; 
       pp.LineJoin = LineJoin.Round; 
       e.DrawPath(pp, pb1gp); 
      } 
     } 
+1

你永遠不應該對'的createGraphics()'借鑑。相反,您需要在Paint事件中繪製所有內容。 – SLaks

+0

'pictureBox1.Image = null;' – stuartd

+0

您將這些點保存在列表中。你不再需要它們,只需使用列表的Clear()方法即可。並調用picturebox的Invalidate()方法讓它重繪自己。 –

回答

0

設置圖像屬性設置爲null應該工作。

picBox.Image = null; 

如果它不工作,您可能會使用InitialImage屬性來顯示您的圖像。

pictBox.InitialImage = null; 

請參考鏈接: Clear image on picturebox

0

這是工作:

private void button4_Click(object sender, EventArgs e) 
     { 
      Graphics graphics; 
      graphics = pictureBox1.CreateGraphics(); 
      graphics.DrawImage(pictureBox1.Image, 0, 0); 
      pb1points = new List<PointF>(); 
     } 
相關問題