首页 > 解决方案 > 优化 WinForm 图形和 DrawLine

问题描述

我目前正在开发 winform 可执行文件的一部分,以随着时间的推移显示跟踪的事件,并且在我一直在进行的小型测试中一切正常。但是,我在处理全尺寸数据集时注意到了一个怪癖:在给定轨道上发生大约 5,000 个事件后,用户可以在一秒钟内看到沿线绘制的轨道。

至少对我而言,这意味着 DrawLine 方法正在为正在绘制的每个单独的线段完成一个完整的 IO 操作,这肯定会在我预期的某些情况下将代码减慢到有问题的水平。这是代码:

void MyProject::DrawTracks(MyTypes::Track_t ^Track, System::Drawing::Font^ Font)
{
    //DisplayPanel is a System::Windows::Form::Panel scaled up to display graphics on
    System::Drawing::Graphics ^graphic = DisplayPanel->CreateGraphic();

    Pen = gcnew System::Drawing::Pen(Track->Color, 2);

    System::Drawing::Point PT, PB;

    PointTop = System::Drawing::Point(0, Track->PosY - 3);
    PointBot = System::Drawing::Point(0, Track->PosY + 3);

    for (unsigned int i = 0; i<Track->nRecs; i++)
    {
       PointTop.X = CalculateTrackXPos(i, Track);
       PointBot.X = PointTop.X;

       graphic->DrawLine(Pen, PT, PB);
    }
}

我最初的印象是使用像 DrawRectangles 这样的批量绘图方法会起作用,但这对性能没有显着影响。在此之后,我在这里复制了用户 ng5000 提交的 SuspendDrawing 和 ResumeDrawing 方法,以便在我绘制线条时暂停更新,从而产生以下代码:

void MyProject::DrawTracks(MyTypes::Track_t ^Track, System::Drawing::Font^ Font)
{
    //DisplayPanel is a System::Windows::Form::Panel scaled up to display graphics on
    System::Drawing::Graphics ^graphic = DisplayPanel->CreateGraphic();

    Pen = gcnew System::Drawing::Pen(Track->Color, 2);

    System::Drawing::Point PT, PB;

    PointTop = System::Drawing::Point(0, Track->PosY - 3);
    PointBot = System::Drawing::Point(0, Track->PosY + 3);

    SuspendDrawing(DisplayPanel);
    for (unsigned int i = 0; i<Track->nRecs; i++)
    {
       PointTop.X = CalculateTrackXPos(i, Track);
       PointBot.X = PointTop.X;

       graphic->DrawLine(Pen, PT, PB);
    }
    ResumeDrawing(DisplayPanel);
}

Suspend 和 Resume Drawing 都阻止了 DrawLine 对显示的图形进行任何更新,这让我很好奇。如何使用 .NET 的图形库绘制一系列大约 11,000 条线,而不必为每条线执行昂贵的 I/O 操作?

标签: .netwinformsoptimizationgraphicsc++-cli

解决方案


推荐阅读