2010-03-06 225 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