1

我正在尝试在 InkCanvas 上创建 1 个复杂的复合形状,但我一定做错了,正如我所期望的那样,事实并非如此。我已经尝试了几种不同的化身来实现这一点。

所以我有这个方法。

    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);
    }

但这就是我得到的:http: //img72.imageshack.us/img72/1286/actual.png

那么我哪里错了?

TIA,埃德

4

1 回答 1

2

问题是 stroke.GetGeometry() 返回的 Geometry 是笔画周围的路径,所以你用黄色填充的区域只是笔画的中间。如果将线条加粗,您可以更清楚地看到这一点:

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

如果您自己将触控笔点列表转换为 StreamGeometry,您可以做任何您想做的事情:

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);
于 2010-06-22T12:49:19.393 回答