excuse me for my bad english. I'm trying to draw a rotating lines on picturebox by setting Paint event handler on it in timer tick handler function:
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Paint += new PaintEventHandler(Draw);//1
foreach (Line line in lines)//array "lines" contains just 16 objects
{
//calculating new coordinates ...
}
}
The line, tagged with "1", is executing slower and slower in process of time. Here is the "Draw" function code below:
void Draw(object sender, PaintEventArgs e)
{
foreach (Line line in lines)
{
e.Graphics.DrawLine(new Pen(Brushes.Black, 5f), line.P1, line.P2);
e.Graphics.FillEllipse(Brushes.Red, line.P1.X - 2.5f, line.P1.Y - 2.5f, 5, 5);
e.Graphics.FillEllipse(Brushes.Red, line.P2.X - 2.5f, line.P2.Y - 2.5f, 5, 5);
e.Graphics.DrawEllipse(new Pen(Brushes.Red, 5f), line.P1.X, line.P1.Y, 1, 1);
e.Graphics.DrawEllipse(new Pen(Brushes.Red, 5f), line.P2.X, line.P2.Y, 1, 1);
}
}
Can you guys please tell me , how can I fix this problem? Thanks!
Do not do pictureBox1.Paint += new PaintEventHandler(Draw);//1 in timer1_Tick. Do it once in your form load. Otherwise it will call Draw() many times per each Paint event and the number of calls will increase.