【问题标题】:(Composite) Geometry confusion in c#(复合)c#中的几何混淆
【发布时间】:2010-06-21 20:32:58
【问题描述】:

我正在尝试在 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, 埃德

【问题讨论】:

标签: c# wpf inkcanvas


【解决方案1】:

问题是 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);

【讨论】:

  • 好的,谢谢!那就是啊哈!那一刻就在那里..笔画几何是笔画周围的路径。现在一切都说得通了,而且也有效!再次感谢!
猜你喜欢
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 2010-12-24
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多