当Pen具有除Null之外的DashStyle时,StreamGeometry不会被栅格化

时间:2012-01-26 08:49:36

标签: c# wpf rendering

我试图创建一个绘制几何图形的画笔。在我尝试将Dashing添加到形状之前,一切正常。

我发现当我使用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;
}

当这样做时,没有任何东西被吸引。如果我不使用破折号(或将破折号设置为null),它可以正常工作。如果我使用StreamGeometry.Parse(geom)并保持潇洒,它也有效。

试图致电sgc.Close()没有帮助。目前我唯一的解决方法是致电:

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

这不是很好......

我错过了什么?

1 个答案:

答案 0 :(得分:4)

这是你遇到的一个非常迷人的错误,我可以确认一下。一些ILSpy挖掘揭示了原因:Geometry.Parse生成的隐式BeginFigure调用将isFilled参数设置为true,而在显式StreamGeometryContext调用中将其设置为false。将sgc.BeginFigure中的第二个参数从false更改为true,虚线将呈现。

WPF路径标记语法不允许指定是否应该填充任何单个数字,所以我想这就是为什么解析器默认填充所有数字,只是为了确定。但我看不出有什么理由说明虚线需要填充数字,这必须是WPF错误。