2010-03-06 228 views
8

我在WPF绘图性能方面存在问题。有很多小的EllipseGeometry对象(例如1024个椭圆),它们被添加到三个不同的前景画笔的独立GeometryGroup中。之后,我把它全部放在简单的图像控制上。代码:带有大量几何图形的WPF绘图性能

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); 

它工作正常,但需要太多的时间 - > 0.5秒上的Core 2 Quad,>奔腾4 2S我需要< 0.1S无处不在。所有的Ellipses,你如何看,都是平等的。控制的背景,我的DisplayImage在哪里,是坚实的(例如黑色),所以我们可以使用这个事实。我试图使用1024 Ellipse元素,而不是使用EllipseGeometries的Image,它的工作速度更快(〜0.5s),但还不够。如何加快它?

问候, 奥列格·雷米弗

附:对不起我的英语不好。

+6

无需为您的英语道歉。 – 2010-03-06 19:10:34

+0

您是否尝试过使用WPF Performance Suit来了解大部分时间需要什么? – levanovd 2010-03-06 19:13:12

+0

谢谢,levanovd,但我知道,我的代码是不正确的,这就够了。我正在问的方法很不一样。 – 2010-03-07 11:43:30

回答

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它的作品真快,即使在Pentium III系统上。

1

即使我有点迟到:This Charles Petzold的帖子在类似的场景中帮了很多忙。

+0

感谢您的回答,我将来可能会在另一个项目中使用它。我真正的解决方案工作正常,所以我认为我保持原样。 – 2010-06-03 15:44:23