2012-01-26 23 views
3

我試圖創建繪製幾何圖形的筆刷。一切工作正常,直到我試圖添加Dashing的形狀。當筆具有DashStyle而非Null時,StreamGeometry不會被柵格化

我發現當我使用Geometry.Parse創建幾何時,虛線顯示正確,但是當我直接使用StreamGeometryContext創建它時,沒有任何東西被渲染。

這是我使用的代碼:

private void RenderGeometryAndSetAsBackground() 
{ 
    Point startPoint = new Point(3505961.52400725, 3281436.57325874); 
    Point[] points = new Point[] { 
     new Point(3503831.75515445,3278705.9649394), 
     new Point(3503905.74802898,3278449.37713916), 
     new Point(3507242.57331039,3276518.41148474), 
     new Point(3507700.6914325,3276536.23547958), 
     new Point(3510146.73449577,3277964.12812859), 
     new Point(3509498.96473447,3278678.60178448), 
     new Point(3507412.1889951,3277215.64022219), 
     new Point(3504326.22698001,3278682.85514017), 
     new Point(3506053.34789057,3281390.66371786)}; 

    string geom = "M3505961.52400725,3281436.57325874L3503831.75515445,3278705.9649394 3503905.74802898,3278449.37713916 3507242.57331039,3276518.41148474 3507700.6914325,3276536.23547958 3510146.73449577,3277964.12812859 3509498.96473447,3278678.60178448 3507412.1889951,3277215.64022219 3504326.22698001,3278682.85514017 3506053.34789057,3281390.66371786"; 
    //Geometry geometry = StreamGeometry.Parse(geom); 

    StreamGeometry geometry = new StreamGeometry(); 
    using (StreamGeometryContext sgc = geometry.Open()) 
    { 
     sgc.BeginFigure(startPoint, false, true); 
     foreach (Point p in points) 
     { 
      sgc.LineTo(p, true, true); 
     } 
    } 

    Pen pen = new Pen(Brushes.Yellow, 3); 
    pen.DashStyle = new DashStyle(new double[] { 30, 30 }, 0); 
    //GeometryDrawing gd = new GeometryDrawing(null, pen, path.RenderedGeometry); 
    GeometryDrawing gd = new GeometryDrawing(null, pen, geometry); 
    DrawingBrush drawingBrush = new DrawingBrush(gd); 
    DrawingBrush tile = drawingBrush.Clone(); 
    tile.Viewbox = new Rect(0.5, 0, 0.25, 0.25); 
    RenderTargetBitmap rtb = new RenderTargetBitmap(256, 256, 96, 96, PixelFormats.Pbgra32); 

    var drawingVisual = new DrawingVisual(); 
    using (DrawingContext context = drawingVisual.RenderOpen()) 
    { 
     context.DrawRectangle(tile, null, new Rect(0, 0, 256, 256)); 
    } 
    rtb.Render(drawingVisual); 

    ImageBrush bgBrush = new ImageBrush(rtb); 
    Background = bgBrush; 
} 

當這樣做的方式,沒有什麼是越來越繪製。如果我不使用衝刺(或將衝刺設置爲空),它可以正常工作。如果我使用StreamGeometry.Parse(geom)並保持速度,它也可以。

試圖撥打sgc.Close()沒有幫助。目前我唯一的解決方法是調用:

geometry = Geometry.Parse(geometry.ToString()); 

這是不是很不錯...

我缺少什麼?

回答

4

這是一個非常迷人的錯誤,你到了那裏,我可以證實它。一些ILSpy挖掘揭示了原因:由Geometry.Parse生成的隱式BeginFigure調用將isFilled參數設置爲true,而在顯式StreamGeometryContext調用中將其設置爲false。將sgc.BeginFigure中的第二個參數從false更改爲true,並且將渲染虛線。

WPF路徑標記語法不允許指定是否應該填充任何單個圖形,所以我想這就是爲什麼解析器默認將它們全部填滿,只是爲了確定。但我看不到任何充分的理由,爲什麼虛線需要填充數字,這必須是一個WPF錯誤。

相關問題