绘制很多线条非常慢

时间:2018-05-14 14:24:12

标签: c# forms draw

我想在3个表单上绘制很多行但是当我调用这个方法时,需要花费很多时间。 200多行,大约需要1秒。我做错了什么?

public void drawObjects()
{
    pAux = selectedPen;
    for (int i = 0; i < objects.Count; i++)
    {
        for(int ed = 0; ed < objects[i].getEdges().Count; ed++)
        {
            frontGraphics.DrawRectangle(recPen, (objects[i].getEdges()[ed].getPoint1().X + 332), -(objects[i].getEdges()[ed].getPoint1().Y - 170), 2, 2);
            rightGraphics.DrawRectangle(recPen, objects[i].getEdges()[ed].getPoint1().Z + 332, -(objects[i].getEdges()[ed].getPoint1().Y - 170), 2, 2);
            topGraphics.DrawRectangle(recPen, objects[i].getEdges()[ed].getPoint1().X + 332, -(objects[i].getEdges()[ed].getPoint1().Z - 170), 2, 2);

            if (objects[i].getEdges().Count > 0)
            {
                if (objects[i].selected == true)
                    pAux = selectedPen;
                else
                    pAux = linePen;
                frontGraphics.DrawLine(pAux, objects[i].getEdges()[ed].getPoint1().X + 332, -(objects[i].getEdges()[ed].getPoint1().Y -170), 
                                           objects[i].getEdges()[ed].getPoint2().X + 332, -(objects[i].getEdges()[ed].getPoint2().Y -170));
                rightGraphics.DrawLine(pAux, objects[i].getEdges()[ed].getPoint1().Z + 332, -(objects[i].getEdges()[ed].getPoint1().Y - 170),
                                            objects[i].getEdges()[ed].getPoint2().Z + 332, -(objects[i].getEdges()[ed].getPoint2().Y - 170));
                topGraphics.DrawLine(pAux, objects[i].getEdges()[ed].getPoint1().X + 332, -(objects[i].getEdges()[ed].getPoint1().Z - 170),
                                           objects[i].getEdges()[ed].getPoint2().X + 332, -(objects[i].getEdges()[ed].getPoint2().Z - 170));

            }
        }          
    }
}

2 个答案:

答案 0 :(得分:2)

目前尚不清楚您正在使用的前端(WPF,WinForms),但您可能会在当前代码中使用大量资源。 getEdges()和/或getPoint()方法可能是昂贵的方法,您可以将这些方法称为很多

如果您要重用方法的结果:将其存储在变量中。这是您的代码的样子:

public void drawObjects()
{
    pAux = selectedPen;
    for (int i = 0; i < objects.Count; i++)
    {
        var currentObject = objects[i];
        var edges = currentObject.getEdges();

        for(int ed = 0; ed < edges.Count; ed++)
        {
            var currentEd = edges[ed];
            var edPoint1 = currentEd.getPoint1();
            var edPoint2 = currentEd.getPoint2();

            frontGraphics.DrawRectangle(recPen, (edPoint1.X + 332), -(edPoint1.Y - 170), 2, 2);
            rightGraphics.DrawRectangle(recPen, edPoint1.Z + 332, -(edPoint1.Y - 170), 2, 2);
            topGraphics.DrawRectangle(recPen, edPoint1.X + 332, -(edPoint1.Z - 170), 2, 2);

            pAux = currentObject.selected ? selectedPen : linePen;

            frontGraphics.DrawLine(pAux, edPoint1().X + 332, -(edPoint1().Y -170), edPoint2.X + 332, -(edPoint2.Y -170));
            rightGraphics.DrawLine(pAux, edPoint1.Z + 332, -(edPoint1.Y - 170), edPoint2.Z + 332, -(edPoint2.Y - 170));
            topGraphics.DrawLine(pAux, edPoint1.X + 332, -(edPoint1.Z - 170), edPoint2.X + 332, -(edPoint2.Z - 170));
        }          
    }
}

编辑,如@Hans Kesting所述,内循环中的这一行是多余的:

if (objects[i].getEdges().Count > 0) 

自从你进入内循环后,它将始终评估为true

PS:请注意,这可能无法解决您的所有性能问题,但我相信这会有所帮助。

答案 1 :(得分:0)

我使用PICTURE BOX解决了这个问题,我在一个位图中绘制并将位图设置为图片框的图像。