代码之家  ›  专栏  ›  技术社区  ›  Ed Z

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

  •  1
  • Ed Z  · 技术社区  · 15 年前

    我正试图在画布上创建一个复杂的复合形状,但我肯定做了一些错误的事情,因为我所期望的事情并不是这样的。我尝试过几种不同的化身来实现这一点。

    所以我有这个方法。

        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

    那我哪里出错了?

    蒂亚 预计起飞时间

    1 回复  |  直到 15 年前
        1
  •  2
  •   Quartermeister    15 年前

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