优化WinForm图形和DrawLine

时间:2018-07-10 18:16:07

标签: .net winforms optimization graphics c++-cli

我目前正在处理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 here提交的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);
}

挂起和恢复绘图都使DrawLine根本无法对显示的图形进行任何更新,这令我感到好奇。如何使用.NET的图形库绘制一系列大约11,000条线,而不必为每条线执行昂贵的I / O操作?

0 个答案:

没有答案