C# Can';t在循环中画到一个画框

C# Can';t在循环中画到一个画框,c#,C#,我有一个列表,其中点包含X和Y 我想要的是循环一个这样的列表,并点到点地画一条线,我通过以下方式来实现: foreach (List<Point> wps in map.waypoints) { System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Black); System.Drawing.Graphics formGraphics = this.pictureBox

我有一个
列表
,其中点包含X和Y

我想要的是循环一个这样的列表,并点到点地画一条线,我通过以下方式来实现:

foreach (List<Point> wps in map.waypoints)
{
  System.Drawing.Pen myPen;
  myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
  System.Drawing.Graphics formGraphics = this.pictureBox1.CreateGraphics();

  Point startPos = new Point(wps[0].X, wps[0].Y);

  foreach (Point p in wps)
  {
    formGraphics.DrawLine(myPen, startPos.X, startPos.Y, p.X, p.Y);
    startPos = p;
  }

  myPen.Dispose();
  formGraphics.Dispose();
}
foreach(在地图航路点中列出wps)
{
System.Drawing.Pen myPen;
myPen=新的System.Drawing.Pen(System.Drawing.Color.Black);
System.Drawing.Graphics formGraphics=此.pictureBox1.CreateGraphics();
点startPos=新点(wps[0].X,wps[0].Y);
foreach(wps中的p点)
{
formGraphics.抽绳(myPen、startPos.X、startPos.Y、p.X、p.Y);
startPos=p;
}
myPen.Dispose();
formGraphics.Dispose();
}

但是什么都没画出来!我对pictureBox的on_click事件也做了同样的处理,但是如果循环一些点,我只使用了鼠标X和Y。我已经验证了点列表,它们不包含rubish:D

在paint事件中编写代码,这样它将引用。picturebox.Invalidate()将调用Paint()

它适合你的代码

      private void pictureBox1_Paint(object sender, PaintEventArgs e) 
   { 
         System.Drawing.Pen myPen; 
            myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
        foreach (List<Point> wps in map.waypoints)
        {  
            Point startPos = new Point(wps[0].X, wps[0].Y);  
            foreach (Point p in wps)  
            {      
                e.Graphics.DrawLine(myPen, startPos.X, startPos.Y, p.X, p.Y);
                startPos = p;  
            }   
        } 
}

明白了吗?

别忘了调用pictureBox1.Invalidate()或pictureBox1.Refresh(),以确保调用绘制事件。

很抱歉,现在还早,但为什么要处理formGraphics?它是在图片的图形中发现的错误(或解决方法)。我在一个空的画盒上画画。但当我在一个有图片的盒子上画下一幅图片时,它完美地展示了:你这样做根本是错误的。最小化窗口并将其还原:消失。拉杰什向你展示了如何正确操作。@janson:这里我只提供了我自己的代码来解释它。根据你的要求更改它。我如何使用e.Graphics.DrawLine(…)?未定义e您在pictureBox1_Paint()中有参数(PaintEventArgs e)。请检查它是否添加了事件:this.pictureBox1.Paint+=new System.Windows.Forms.PaintEventHandler(pictureBox1_Paint);然后在实现上取消pictureBox1_Paint(对象发送者,System.Windows.Forms.PaintEventArgs e)在UI中选择Picturebox控件并右键单击它。转到属性菜单->事件->绘制->双击它。然后在pictureBox1_Paint事件中编写代码(请参阅上面的文章)。然后调用pictureBox1.invalidate()(这称为绘制事件)
      private void pictureBox1_Paint(object sender, PaintEventArgs e) 
   { 
         System.Drawing.Pen myPen; 
            myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
        foreach (List<Point> wps in map.waypoints)
        {  
            Point startPos = new Point(wps[0].X, wps[0].Y);  
            foreach (Point p in wps)  
            {      
                e.Graphics.DrawLine(myPen, startPos.X, startPos.Y, p.X, p.Y);
                startPos = p;  
            }   
        } 
}
       public void DoFunction()
       { 
         .....
         .....
         pictureBox1.Invalidate() /* here automatic call to pictureBox1_Paint(object sender, PaintEventArgs e) */

         . . . . 

         }