1

我有一个在屏幕上绘制点的功能。此功能运行良好,直到我添加了panelGraphics.RotateTransform. 当这条线存在时,进行一次重绘的过程很长。我的点列表包含大约 5000 个点,如果没有旋转,它会在几毫秒内完成,但使用这条线可能需要 500 毫秒,这非常慢。你知道为什么 RotateTransform 这么慢,还有,我能做些什么来优化它?

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();
}
4

1 回答 1

2

原因是每个矩形都必须旋转。旋转可能是一个缓慢的操作,尤其是对于非直角。

在这种情况下,更好的方法是首先创建一个“隐藏”位图,您可以在其中绘制矩形。然后将旋转应用于主图形对象并将隐藏位图绘制到主位图(控件)上。像这样的东西-

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 = e.Graphics; //use the provided Graphics object

    // create an internal bitmap to draw rectangles to
    Bitmap bmp = new Bitmap(this.ClientSize.Width, _
                            this.ClientSize.Height, _
                            Imaging.PixelFormat.Format32bppPArgb);

    using (Graphics g = Graphics.FromImage(bmp)) {

        PointF ptPrevious = new PointF(float.MaxValue, float.MaxValue);
        foreach (PointF pt in _listPoint) {
            g.FillRectangle(myBrush, (pt.X / 25) * _fZoomFactor, _
                                     (pt.Y / 25) * _fZoomFactor, 2, 2);
        }   
    }

    panelGraphics.TranslateTransform((panel1.ClientSize.Width / 2) + _panW, _
                                     (panel1.ClientSize.Height / 2) + _panH);

    //Problematic line...
    panelGraphics.RotateTransform(230 - Convert.ToInt32(_dPan), _
                                  System.Drawing.Drawing2D.MatrixOrder.Prepend);

    panelGraphics.DrawImage(bmp, 0, 0); //adjust x/y as needed
    bmp.Dispose;

    myBrush.Dispose();
    myPen.Dispose();
}
于 2012-12-17T17:04:49.070 回答