使用路径时渲染速度较慢

时间:2014-04-16 01:54:15

标签: winforms forms paint onpaint

为什么使用路径时下面的代码渲染速度要慢得多,而不是单独绘制每一行?

如果我单独绘制每一行,窗口大小调整似乎没问题,但如果我使用路径,则调整大小非常滞后。

public class GdiRenderCanvas : System.Windows.Forms.Control
        {
            System.Drawing.Drawing2D.GraphicsPath path = null;

            public GdiRenderCanvas()
            {
                SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.UserPaint, true);
                DoubleBuffered = true;
                ResizeRedraw = true;

                path = new System.Drawing.Drawing2D.GraphicsPath();
                Random r = new Random(8);
                for (int i = 0; i < 1000; ++i)
                {
                    int x = (int)(r.NextDouble() * 800);
                    int y = (int)(r.NextDouble() * 800);

                    int x1 = (int)(r.NextDouble() * 800);
                    int y1 = (int)(r.NextDouble() * 800);

                    path.AddLine(x, y, x1, y1);
                    path.CloseFigure();
                }
            }

            protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
            {
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;

                if (false)
                {
                    Random r = new Random(8);
                    for (int i = 0; i < 1000; ++i)
                    {
                        int x = (int)(r.NextDouble() * 800);
                        int y = (int)(r.NextDouble() * 800);

                        int x1 = (int)(r.NextDouble() * 800);
                        int y1 = (int)(r.NextDouble() * 800);

                        e.Graphics.DrawLine(pen, x, y, x1, y1);
                    }
                }
                else
                {
                    e.Graphics.DrawPath(pen, path);
                }
            }

            System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);
        }

0 个答案:

没有答案