优化WPF DrawingContext.DrawLine
在WPF中,除了使用优化WPF DrawingContext.DrawLine,wpf,performance,drawingcontext,Wpf,Performance,Drawingcontext,在WPF中,除了使用 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=新建DrawingVisual();
DrawingContext dc=dv.renderropen();
foreach(SerieVM SerieVM in _曲线系列){
画笔系列Pen=新画笔(serieVm.Stroke,1.0);
点lastDrawnPoint=新点();
布尔第一点=真;
foreach(serieVm.Points.Cast()中的CurveValuePointVM-pointVm){
如果(pointVm.XValuexMax)继续;
double x=基点.x+(pointVm.XValue-xMin)*xSizePerValue;
双y=basePoint.y-(pointVm.Value-yMin)*ySizePerValue;
点坐标=新点(x,y);
如果(第一点){
第一点=假;
}否则{
dc.抽绳(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();
DrawingVisual dv=新建DrawingVisual();
DrawingContext dc=dv.renderropen();
foreach(SerieVM SerieVM in _曲线系列){
StreamGeometry g=新的StreamGeometry();
StreamGeometryContext sgc=g.Open();
画笔系列Pen=新画笔(serieVm.Stroke,1.0);
布尔第一点=真;
foreach(serieVm.Points.Cast()中的CurveValuePointVM-pointVm){
如果(pointVm.XValuexMax)继续;
double x=基点.x+(pointVm.XValue-xMin)*xSizePerValue;
双y=basePoint.y-(pointVm.Value-yMin)*ySizePerValue;
点坐标=新点(x,y);
如果(第一点){
第一点=假;
sgc.Beginigure(坐标、假、假);
}否则{
sgc.LineTo(协调、正确、错误);
}
}
sgc.Close();
dc.图纸几何图形(空,序列号,g);
}
dc.Close();
旧代码绘制两条3000点的曲线需要约140 ms。新版本大约需要5毫秒。使用StreamGeometry似乎比DrawingContext.Drawline更有效
编辑:我使用的是dotnet framework 3.5版这个问题确实很老了,但我找到了一种方法,改进了代码的执行,同时使用了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=新建DrawingVisual();
DrawingContext dc=dv.renderropen();
foreach(SerieVM SerieVM in _曲线系列){
画笔系列Pen=新画笔(serieVm.Stroke,1.0);
点lastDrawnPoint=新点();
布尔第一点=真;
foreach(serieVm.Points.Cast()中的CurveValuePointVM-pointVm){
如果(pointVm.XValuexMax)继续;
double x=基点.x+(pointVm.XValue-xMin)*xSizePerValue;
双y=basePoint.y-(pointVm.Value-yMin)*ySizePerValue;
点坐标=新点(x,y);
如果(第一点){
第一点=假;
}否则{
dc.抽绳(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();
DrawingVisual dv=新建DrawingVisual();
DrawingContext dc=dv.renderropen();
foreach(SerieVM SerieVM in _曲线系列){
StreamGeometry g=新的StreamGeometry();
StreamGeometryContext sgc=g.Open();
画笔系列Pen=新画笔(serieVm.Stroke,1.0);
布尔第一点=真;
foreach(serieVm.Points.Cast()中的CurveValuePointVM-pointVm){
如果(pointVm.XValuexMax)继续;
double x=基点.x+(pointVm.XValue-xMin)*xSizePerValue;
双y=basePoint.y-(pointVm.Value-yMin)*ySizePerValue;
点坐标=新点(x,y);
如果(第一点){
第一点=假;
sgc.Beginigure(坐标、假、假);
}否则{
sgc.LineTo(协调、正确、错误);
}
}
sgc.Close();
dc.图纸几何图形(空,序列号,g);
}
dc.Close();
旧代码绘制两条3000点的曲线需要约140 ms。新版本大约需要5毫秒。使用StreamGeometry似乎比DrawingContext.Drawline更有效
编辑:我正在使用dotnet framework 3.5版谢谢,我试过了,它确实提供了一个小的改进。谢谢,我试过了,它确实提供了一个小的改进。