Search code examples
c#wpfinkcanvas

(Composite) Geometry confusion in c#


I'm trying to create 1 complex composite shape on an InkCanvas, but I must be doing something wrong, as what I was expecting to happen, is not. I've tried several different incarnations of accomplishing this.

So I have this method.

    private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
    {
        Stroke stroke = e.Stroke;

        // Close the "shape".
        StylusPoint firstPoint = stroke.StylusPoints[0];
        stroke.StylusPoints.Add(new StylusPoint() { X = firstPoint.X, Y = firstPoint.Y });

        // Hide the drawn shape on the InkCanvas.
        stroke.DrawingAttributes.Height = DrawingAttributes.MinHeight;
        stroke.DrawingAttributes.Width = DrawingAttributes.MinWidth;

        // Add to GeometryGroup. According to http://msdn.microsoft.com/en-us/library/system.windows.media.combinedgeometry.aspx
        // a GeometryGroup should work better at Unions.
        _revealShapes.Children.Add(stroke.GetGeometry());

        Path p = new Path();
        p.Stroke = Brushes.Green;
        p.StrokeThickness = 1;
        p.Fill = Brushes.Yellow;
        p.Data = _revealShapes.GetOutlinedPathGeometry();

        selectionInkCanvas.Children.Clear();        
        selectionInkCanvas.Children.Add(p);
    }

But this is what I get: http://img72.imageshack.us/img72/1286/actual.png

So where am I going wrong?

TIA, Ed


Solution

  • The problem is that the Geometry returned by stroke.GetGeometry() is a path around the stroke, so the area you're filling with yellow is just the middle of the stroke. You can see this more clearly if you make the lines thicker:

    _revealShapes.Children.Add(stroke.GetGeometry(new DrawingAttributes() { Width = 10, Height = 10 }));
    

    You can do what you want if you convert the list of stylus points to a StreamGeometry yourself:

    var geometry = new StreamGeometry();
    using (var geometryContext = geometry.Open())
    {
        var lastPoint = stroke.StylusPoints.Last();
        geometryContext.BeginFigure(new Point(lastPoint.X, lastPoint.Y), true, true);
        foreach (var point in stroke.StylusPoints)
        {
            geometryContext.LineTo(new Point(point.X, point.Y), true, true);
        }
    }
    geometry.Freeze();
    _revealShapes.Children.Add(geometry);