【问题标题】:Rotate a Geometry Path旋转几何路径
【发布时间】:2011-06-17 11:53:09
【问题描述】:

我正在使用Streamgeometry 绘制一个简单的箭头。现在我需要将箭头转到指定的角度。但是如何旋转这个几何图形呢?

Dim pt1 As New Point(X1, Me.Y1) 'left point
Dim pt2 As New Point(_X2, Me.Y2) 'right point

Dim pt3 As New Point(_X2 + (HeadWidth * cost - HeadHeight * sint), Y2 + (HeadWidth * sint + HeadHeight * cost)) 'arrow line down
Dim pt4 As New Point(_X2 + (HeadWidth * cost + HeadHeight * sint), Y2 - (HeadHeight * cost - HeadWidth * sint)) 'arrow line up

context.BeginFigure(pt1, True, False)
context.LineTo(pt2, True, True)
context.LineTo(pt3, True, True)
context.LineTo(pt2, True, True)
context.LineTo(pt4, True, True)

【问题讨论】:

    标签: wpf graphics path geometry


    【解决方案1】:

    如果旋转只是为了展示(即您不关心原始几何数据仍然是指向原始方向的箭头),那么您可以对其应用transform

    绘制完上下文后,只需将转换应用于原始 StreamGeometry 对象(C# 中的代码,但它也适用于 VB.NET):

    var geo = new StreamGeometry();
    using (var ctx = geo.Open())
    {
        ctx.BeginFigure(new Point(0, 20), false, false);
        ctx.LineTo(new Point(100, 20), true, true);
        ctx.LineTo(new Point(80, 40), true, true);
        ctx.LineTo(new Point(80, 0), true, true);
        ctx.LineTo(new Point(100, 20), true, true);
    }
    geo.Transform = new RotateTransform(45);
    var drawing = new GeometryDrawing(Brushes.Transparent, new Pen(Brushes.Black, 1), geo);
    image1.Source = new DrawingImage(drawing);
    

    上面的代码将在名为image1Image 控件上绘制一个向下/向右的箭头。

    【讨论】:

    • 感谢您的回答。我试过了,但如果我像你建议的那样使用带有 Rotatetransform 的几何图形,它就没有任何效果。我不知道为什么。
    猜你喜欢
    • 2014-02-19
    • 1970-01-01
    • 2014-09-22
    • 2020-02-06
    • 2015-02-26
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多