(复合)c#中的几何混淆

时间:2010-06-21 20:32:58

标签: c# wpf inkcanvas

我正在尝试在InkCanvas上创建一个复杂的复合形状,但我必须做错事,正如我期望发生的那样,不是。我已经尝试了几种不同的化身来实现这一目标。

所以我有这个方法。

    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, 编

1 个答案:

答案 0 :(得分:2)

问题是stroke.GetGeometry()返回的几何是一个围绕笔划的路径,所以你用黄色填充的区域就是笔划的中间位置。如果你将线条变粗,你可以更清楚地看到这一点:

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