I'm developing a UWP application using Win2D, and I need to rotate text using the CanvasControl Drawing session. I used public void DrawText(string text, Vector2 point, Color color)
to render text.
How can I rotate the text to any angle?
In Win2D, rotating text can be achieved by setting CanvasDrawingSession.Transform
.
private void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
CanvasDrawingSession ds = args.DrawingSession;
ds.Transform = Matrix3x2.CreateRotation(GetRadians(30));
ds.DrawText("Hello Win2D!", 10, 10, Colors.Red);
}
private float GetRadians(double angle)
{
return (float)(Math.PI * angle / 180.0);
}
It is worth mentioning that the parameter of Matrix3x2.CreateRotation
is in radians. If you like to express in angle, you need to convert.
The rotation here is centered on the upper left corner of the canvas, if you need to customize the center of rotation, you can write like this:
ds.Transform = Matrix3x2.CreateRotation(GetRadians(30),new Vector2(30,30));