Rotate a Geometry Path
I'm working newly with a Streamgeometry to draw a simple arrow. Now I need to turn the arrow to a specified angle. But how to rotate this geometry?
Dim pt1 As New Point(X1, Me.Y1) 'left point
Dim pt2 As New Point(开发者_如何学Python_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)
If the rotation is only for presentation (i.e. you don't care that the original geometry data is still an arrow pointing in the original direction) then you can apply a transform to it.
After you've drawn on your context, just apply the transform on the original StreamGeometry object (code in C# but it applies to VB.NET too):
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);
The above code will draw an arrow pointing down/right on an Image
control named image1
.
精彩评论