I have a function that draw points on the screen. This function worked very well until I added the line with panelGraphics.RotateTransform. When this line is there, the process of doing one repaint is very long. My list of points contains about 5000 points and without the rotation, it’s done in a couple of milliseconds but with that line it could take up to 500 ms which is VERY slow. Do you have any idea why RotateTransform is so slow and also, what can I do to optimize this?
private void panel1_Paint(object sender, PaintEventArgs e)
{
Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
Graphics panelGraphics = panel1.CreateGraphics();
panelGraphics.TranslateTransform((panel1.Width / 2) + _panW, (panel1.Height / 2) + _panH);
//Problematic line...
panelGraphics.RotateTransform(230 - Convert.ToInt32(_dPan), System.Drawing.Drawing2D.MatrixOrder.Prepend);
PointF ptPrevious = new PointF(float.MaxValue, float.MaxValue);
foreach (PointF pt in _listPoint)
{
panelGraphics.FillRectangle(myBrush, (pt.X / 25) * _fZoomFactor, (pt.Y / 25) * _fZoomFactor, 2, 2);
}
myBrush.Dispose();
myPen.Dispose();
panelGraphics.Dispose();
}
The reason is that each rectangle has to be rotated. Rotation can be a slow operation, especially for none square angles.
The better approach in this case is to first create a “hidden” bitmap which you draw the rectangles into. Then apply the rotation to your main graphics object and draw the hidden bitmap onto your main bitmap (control). Something like this –