我的WPF绘图性能有问题。有许多小的EllipseGeometry对象(例如1024个椭圆),它们被添加到具有不同前景画笔的三个单独的GeometryGroup中。之后,我在简单的图像控件上渲染它。代码:
DrawingGroup tmpDrawing = new DrawingGroup();
GeometryGroup onGroup = new GeometryGroup();
GeometryGroup offGroup = new GeometryGroup();
GeometryGroup disabledGroup = new GeometryGroup();
for (int x = 0; x < DisplayWidth; ++x)
{
for (int y = 0; y < DisplayHeight; ++y)
{
if (States[x, y] == true) onGroup.Children.Add(new EllipseGeometry(new Rect((double)x * EDGE, (double)y * EDGE, EDGE, EDGE)));
else if (States[x, y] == false) offGroup.Children.Add(new EllipseGeometry(new Rect((double)x * EDGE, (double)y * EDGE, EDGE, EDGE)));
else disabledGroup.Children.Add(new EllipseGeometry(new Rect((double)x * EDGE, (double)y * EDGE, EDGE, EDGE)));
}
}
tmpDrawing.Children.Add(new GeometryDrawing(OnBrush, null, onGroup));
tmpDrawing.Children.Add(new GeometryDrawing(OffBrush, null, offGroup));
tmpDrawing.Children.Add(new GeometryDrawing(DisabledBrush, null, disabledGroup));
DisplayImage.Source = new DrawingImage(tmpDrawing);
它工作正常,但需要花费太多时间 - 在Core 2 Quad上大于0.5秒,在Pentium 4上大于2秒。我需要&lt; 0.1s无处不在。你可以看到的所有椭圆都是平等的。控件的背景,我的DisplayImage,是固体(例如黑色),所以我们可以使用这个事实。我尝试使用1024个Ellipse元素而不是使用EllipseGeometries的Image,并且它工作得更快(~0.5s),但还不够。如何加快它?
此致 Oleg Eremeev
P.S。抱歉我的英文。
答案 0 :(得分:4)
我离开了旧的渲染方法,但每次创建新的EllipseGeometry对象都是个坏主意,所以我用这种方式优化它:
for (int x = 0; x < newWidth; ++x)
{
for (int y = 0; y < newHeight; ++y)
{
States[x, y] = null;
OnEllipses[x, y] = new EllipseGeometry(new Rect((double)x * EDGE + 0.5f, (double)y * EDGE + 0.5f, EDGE - 1f, EDGE - 1f));
OffEllipses[x, y] = new EllipseGeometry(new Rect((double)x * EDGE + 0.5f, (double)y * EDGE + 0.5f, EDGE - 1f, EDGE - 1f));
DisabledEllipses[x, y] = new EllipseGeometry(new Rect((double)x * EDGE + 0.5f, (double)y * EDGE + 0.5f, EDGE - 1f, EDGE - 1f));
}
}
// . . .
DrawingGroup tmpDrawing = new DrawingGroup();
GeometryGroup onGroup = new GeometryGroup();
GeometryGroup offGroup = new GeometryGroup();
GeometryGroup disabledGroup = new GeometryGroup();
for (int x = 0; x < DisplayWidth; ++x)
{
for (int y = 0; y < DisplayHeight; ++y)
{
if (States[x, y] == true) onGroup.Children.Add(OnEllipses[x, y]);
else if (States[x, y] == false) offGroup.Children.Add(OffEllipses[x, y]);
else disabledGroup.Children.Add(DisabledEllipses[x, y]);
}
}
tmpDrawing.Children.Add(new GeometryDrawing(OnBrush, null, onGroup));
tmpDrawing.Children.Add(new GeometryDrawing(OffBrush, null, offGroup));
tmpDrawing.Children.Add(new GeometryDrawing(DisabledBrush, null, disabledGroup));
DisplayImage.Source = new DrawingImage(tmpDrawing);
对于x = 128和y = 8,即使在奔腾III系统上也能正常运行。
答案 1 :(得分:1)
即使我有点迟了,Charles Petzold的This帖子在类似情况下帮了很多。