0
votes

I want to add some text on a canvas and decided to use textBlock for that (to set Font etc.) But I cannot figure out how to rotate it. I use the following function to add myText on myCanvas:

void text(double x_pos, double y_pos, string myText, double angle, Point rot_cen, Color color1)
        {
            TextBlock textBlock = new TextBlock()
            {
                Text = myText,
                FontFamily = new FontFamily("Verdana"),
                FontSize = 16,
                TextAlignment = TextAlignment.Center
            };
            textBlock.Foreground = new SolidColorBrush(color1);
            Canvas.SetLeft(textBlock, x_pos);
            Canvas.SetTop(textBlock, y_pos);

            textBlock.RenderTransform = new RotateTransform(angle, rot_cen.X, rot_cen.Y);

            myCanvas.Children.Add(textBlock);
        }

From what I've read rot_cen is a point from (0,0) - which is top left corner to (1,1) - which is bottom right corner. But when i set it to be (0.5,0.5) it still rotates around top left corner. Do I need to update it somehow?

1

1 Answers

0
votes

The CenterX and CenterY properties of a RotateTransform use absolute coordinates.

You may want to set RenderTransformOrigin, which uses relative coordinates:

textBlock.RenderTransformOrigin = rot_cen;
textBlock.RenderTransform = new RotateTransform(angle);