在WPF中有比使用更有效的方法来绘制线条吗
DrawingContext.DrawLine(pen, a, b); ?我在我的应用程序中做了大量的线条绘制,99%的时间都花在了一个循环中,进行这个调用。
a,b来自一个非常大的不断变化的点列表。我不需要任何输入反馈/事件或任何类似的东西,...我只需要快速绘制这些点即可。
有什么建议吗?
发布于 2012-11-12 16:02:10
你可以试着把笔冻起来。这是关于freezable objects的概述。
发布于 2014-10-13 19:33:33
这个问题真的很老了,但我找到了一种方法来改善我的代码的执行,它也使用了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版
发布于 2012-11-14 03:37:45
看起来StreamGeometry是个不错的选择。即使没有冻结它,我仍然获得了性能改进。
https://stackoverflow.com/questions/13335841
复制相似问题