快速的方式提请使用不同的颜色使用GDI +线?颜色、不同、快速、方式

2023-09-05 02:32:57 作者:pretend △假装

我点的动态列表,新的点可以随时加入。我想画线用不同的颜色来连接它们。颜色是基于这些点的索引。这里是code:

 私人列表<点> _points;
    私有静态笔PEN1 =新钢笔(Color.Red,10);
    私有静态笔PEN2 =新钢笔(Color.Yellow,10);
    私有静态笔PEN3 =新钢笔(Color.Blue,10);
    私有静态笔pen4 =新钢笔(Color.Green,10);

    私人无效的init()
    {
        //使用固定的80 simpicity
        _points =新名单,其中;点>(80);

        的for(int i = 0; I< 80;我++)
        {
            _points.Add(新点(30 + I * 10,30));
        }
    }

    私人无效DrawLinesNormal(PaintEventArgs的E)
    {
        的for(int i = 0; I< _points.Count-1;我++)
        {
            如果(ⅰ小于20)
                e.Graphics.DrawLine(PEN1,_points [I],_points [I + 1]);
            否则如果(ⅰ&所述; 40)
                e.Graphics.DrawLine(PEN2,_points [I],_points [I + 1]);
            否则如果(ⅰ&所述; 60)
                e.Graphics.DrawLine(PEN3,_points [I],_points [I + 1]);
            其他
                e.Graphics.DrawLine(pen4,_points [I],_points [I + 1]);
        }
    }
 

我觉得这个方法不够快,当我有新的点进来的高速。有没有什么办法,使其更快?我做了一些研究和使用的GraphicsPath可能会更快,有人说,但如何?

[更新]我收集了一些可能的优化:

使用GrahpicsPath,Original问 在改变图形质量(如Smoothi​​ngMode / PixelOffsetMode ......),也称对setClip指定的唯一必要的区域来呈现。 解决方案

您将无法挤出更多的速度超出了code,而不会失去质量或更换,以更快的渲染器(GDI,OpenGL的,支持DirectX )。但GDI通常是相当快一点(也许2个),以及DirectX / OpenGL的速度会更快(也许10倍),这取决于你画什么。

使用路径的想法是,你批的许多(在你的榜样,20)线到一个单一的方法调用,而不是调用DrawLine的20倍。这将不仅有利于你,如果你可以安排传入的数据转换成列表的点的正确格式的绘图程序。否则,你将不得不点复制到正确的数据结构,这样会浪费很多,你是通过分批进入的路径获得的时间。在DrawPath的情况下,你可能需要从一个点的数组,这可能导致没有保存的时间创建一个GraphicsPath的。但是,如果你要绘制相同的路径超过一次,你可以缓存它,然后你可能会看到一个净效益。

用参考线或对齐将PPT页面划分为平均相等的四个区域 列或行

如果新的点添加到列表中,但旧的不会被删除(即你永远只是增加新的生产线,以显示),那么你将能够使用屏幕外的位图保存至今呈现的线条。这样一个点被添加每次,你画的一个的行,而不是每次都绘制所有80行。

这一切都取决于你想要做什么。

I have an dynamic List of Point, new Point can be added at any time. I want to draw lines to connect them using different color. Color is based on the index of those points. Here is the code:

    private List<Point> _points;
    private static Pen pen1 = new Pen(Color.Red, 10);
    private static Pen pen2 = new Pen(Color.Yellow, 10);
    private static Pen pen3 = new Pen(Color.Blue, 10);
    private static Pen pen4 = new Pen(Color.Green, 10);

    private void Init()
    {
        // use fixed 80 for simpicity
        _points = new List<Point>(80);

        for (int i = 0; i < 80; i++)
        {
            _points.Add(new Point(30 + i * 10, 30));
        }
    }

    private void DrawLinesNormal(PaintEventArgs e)
    {
        for (int i = 0; i < _points.Count-1; i++)
        {
            if (i < 20)
                e.Graphics.DrawLine(pen1, _points[i], _points[i + 1]);
            else if (i < 40)
                e.Graphics.DrawLine(pen2, _points[i], _points[i + 1]);
            else if (i < 60)
                e.Graphics.DrawLine(pen3, _points[i], _points[i + 1]);
            else
                e.Graphics.DrawLine(pen4, _points[i], _points[i + 1]);
        }
    }

I find this method is not fast enough when I have new points coming in at a high speed. Is there any way to make it faster? I did some research and someone said using GraphicsPath could be faster, but how?

[UPDATE] I collect some possible optimizations:

Using GrahpicsPath, Original Question Change Graphics quality ( such as SmoothingMode/PixelOffsetMode...), also call SetClip to specify the only necessary region to render.

解决方案

You won't be able to squeeze much more speed out of that code without losing quality or changing to a faster renderer (GDI, OpenGL, DirectX). But GDI will often be quite a bit faster (maybe 2x), and DirectX/OpenGL can be much faster (maybe 10x), depending on what you're drawing.

The idea of using a Path is that you batch many (in your example, 20) lines into a single method call, rather than calling DrawLine 20 times. This will only benefit you if you can arrange the incoming data into the correct list-of-points format for the drawing routine. Otherwise, you will have to copy the points into the correct data structure and this will waste a lot of the time that you are gaining by batching into a path. In the case of DrawPath, you may have to create a GraphicsPath from an array of points, which may result in no time saved. But if you have to draw the same path more than once, you can cache it, and you may then see a net benefit.

If new points are added to the list, but old ones are not removed (i.e. you are always just adding new lines to the display) then you would be able to use an offscreen bitmap to store the lines rendered so far. That way each time a point is added, you draw one line, rather than drawing all 80 lines every time.

It all depends on exactly what you're trying to do.