我在WPF绘图性能方面遇到了问题。有许多小的EllipseGeometry对象(例如1024个椭圆),它们被添加到三个不同的前景画刷的GeometryGroups中。最后,我将其全部呈现在简单的Image控件上。代码:
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秒。 我需要在每个地方都达到<0.1秒。可以看到,所有的椭圆形都是相等的。控件背景,即我的DisplayImage所在的位置,是纯色的(例如黑色),因此我们可以利用这一事实。我尝试使用1024个椭圆元素代替带有EllipseGeometries的图像,速度提升了很多(~0.5秒),但仍然不够快。如何加速它?
敬礼,Oleg Eremeev
P.S. 对于我的英语表示抱歉。