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

发布于 2024-09-06 04:14:04 字数 1363 浏览 7 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2024-09-13 04:14:04

问题在于,由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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文