2014-03-28 89 views
0

我想在Visual Layer中繪製一個Polyline。這裏是我用來繪製Line的代碼。我應該畫多個Lines並將它們添加到VisualCollection還是有更好的方法?使用c在DrawingContext中繪製折線#

var drawingVisual = new DrawingVisual(); 
using (var dc = drawingVisual.RenderOpen()) 
{ 
    var myPen = new Pen 
    { 
     Thickness = thickness, 
     Brush = Settings.GridColor 
    }; 
    myPen.Freeze(); 
    dc.DrawLine(myPen, pt1, pt2); 
} 
+0

也許你錯過了一些標籤嗎?請更具體一些。 – Dmitry

回答

4

我想你會比DrawLine更好使用DrawGeometry

例子:

var myPen = new Pen 
{ 
    Thickness = thickness, 
    Brush = Settings.GridColor 
}; 
myPen.Freeze(); 


var geometry = new StreamGeometry(); 
using (StreamGeometryContext ctx = geometry.Open()) 
{ 
    ctx.BeginFigure(new Point(10, 100), true /* is filled */, true /* is closed */); 
    ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */); 
    ctx.LineTo(new Point(100, 50), true /* is stroked */, false /* is smooth join */); 
} 
geometry.Freeze(); 

dc.DrawGeometry(null, myPen, geometry); 
1

我不認爲這是一個更好的辦法,但不是調用myPen.Freeze()我只想創建爲const。我不知道這是否真的更有效率,我只是認爲將來會更多地清除代碼。我也會(個人的)不使用var關鍵字,因爲當您致電drawingVisual.RenderOpen()時,您創建的類型會更不明確。