除了使用
DrawingContext.DrawLine(pen, a, b); ?
我在我的应用程序中画了很多线,99% 的时间都花在了循环调用上。
[a,b] 来自大量不断变化的点。我不需要任何输入反馈/事件或类似的东西,...我只需要非常快地绘制点。
有什么建议么?
除了使用
DrawingContext.DrawLine(pen, a, b); ?
我在我的应用程序中画了很多线,99% 的时间都花在了循环调用上。
[a,b] 来自大量不断变化的点。我不需要任何输入反馈/事件或类似的东西,...我只需要非常快地绘制点。
有什么建议么?
您可以尝试冻结笔。这是可冻结对象的概述。
这个问题真的很老,但我找到了一种改进我的代码执行的方法,它也使用了 DrawingContext.DrawLine。
这是我一小时前绘制曲线的代码:
DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
foreach (SerieVM serieVm in _curve.Series) {
Pen seriePen = new Pen(serieVm.Stroke, 1.0);
Point lastDrawnPoint = new Point();
bool firstPoint = true;
foreach (CurveValuePointVM pointVm in serieVm.Points.Cast<CurveValuePointVM>()) {
if (pointVm.XValue < xMin || pointVm.XValue > xMax) continue;
double x = basePoint.X + (pointVm.XValue - xMin) * xSizePerValue;
double y = basePoint.Y - (pointVm.Value - yMin) * ySizePerValue;
Point coord = new Point(x, y);
if (firstPoint) {
firstPoint = false;
} else {
dc.DrawLine(seriePen, lastDrawnPoint, coord);
}
lastDrawnPoint = coord;
}
}
dc.Close();
这是现在的代码:
DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
foreach (SerieVM serieVm in _curve.Series) {
StreamGeometry g = new StreamGeometry();
StreamGeometryContext sgc = g.Open();
Pen seriePen = new Pen(serieVm.Stroke, 1.0);
bool firstPoint = true;
foreach (CurveValuePointVM pointVm in serieVm.Points.Cast<CurveValuePointVM>()) {
if (pointVm.XValue < xMin || pointVm.XValue > xMax) continue;
double x = basePoint.X + (pointVm.XValue - xMin) * xSizePerValue;
double y = basePoint.Y - (pointVm.Value - yMin) * ySizePerValue;
Point coord = new Point(x, y);
if (firstPoint) {
firstPoint = false;
sgc.BeginFigure(coord, false, false);
} else {
sgc.LineTo(coord, true, false);
}
}
sgc.Close();
dc.DrawGeometry(null, seriePen, g);
}
dc.Close();
旧代码需要大约 140 毫秒来绘制两条 3000 个点的曲线。新的大约需要 5 毫秒。使用 StreamGeometry 似乎比 DrawingContext.Drawline 更有效。
编辑:我正在使用 dotnet 框架 3.5 版
看来StreamGeometry是要走的路。即使没有冻结它,我仍然可以获得性能提升。