.net - 优化 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 操作?
解决方案
推荐阅读
- android - 如何添加在其他应用程序中显示的功能
- javascript - 如何避免 React.js 中的这种无限更新循环?
- python - 如何根据另一个表中的数据过滤一个表
- java - 如何在 Android Studio 中修复我的旧应用程序?
- python - Tkinter 中 Canvas 的 tag_bind 方法
- python - TypeError:列表索引必须是整数或切片,而不是 str - Python 代码错误
- python - 遍历数组的最佳实现是什么?
- java - 直接在 IntelliJ IDEA 中运行时,Spring Webflow 在 Spring Boot 应用程序中找不到资源文件夹
- python - 在python中使用numba自引用类似结构的对象
- html - 将 fontawsome 图标移动到表格的底部边框