Search code examples
c#uwpwin2d

How to rotate text while writing it on image using win2D.uwp in C#?


I am using win2d.uwp nuget package for adding water mark on a image and then save it. Like this

drawingSession.DrawImage(image, 0, 0);
drawingSession.DrawText("Sample Text", x, y, txtColor, canvasTxtFormat);

Everything is working fine. I want to rotate this text while writing it on the image and I am having a hard time finding help regarding this online.

Any help will be appreciated.


Solution

  • You will want to use Matrix3x2.CreateRotation to rotate your text. For example, the following code rotates the text in 90 degrees clockwise. Don't forget to specify the center point as in the second parameter.

    drawingSession.Transform *= Matrix3x2.CreateRotation((float)Math.PI / 2, new Vector2(_dimension / 2));
    drawingSession.DrawText("Sample Text", x, y, txtColor, canvasTxtFormat);