0

I want to draw a string and rotate it with a custom angle. Simply, I calculate the new dimenstions of the area that contains the rotated image, then create a bitmap object and by means of graphics object of it, I draw a string with 3 transformations (translation to the center, rotation, and reverse translation). I wrote the following code but the quality is not desired. Does anyone have an idea?

    private Image RotateText(string Text, int FontSize, float Angle)
    {

        //Modify angle
        Angle *= -1;

        //Calculate rotation angle in radian
        double AngleInRadian = (Angle * 2 * Math.PI) / 360d;

        //Instantiate a font for text
        Font TextFont = new Font("Tahoma", FontSize, FontStyle.Bold);

        //Measure size of the text
        Graphics Graphic = this.CreateGraphics();
        SizeF TextSize = Graphic.MeasureString(Text, TextFont);

        //Calculate size of the rotated text
        double NewWidth  = Math.Abs(TextSize.Width * Math.Cos(AngleInRadian)) + Math.Abs(TextSize.Height * Math.Sin(AngleInRadian));
        double NewHeight = Math.Abs(TextSize.Width * Math.Sin(AngleInRadian)) + Math.Abs(TextSize.Height * Math.Cos(AngleInRadian));

        //Instantiate a new image for rotated text
        Bitmap RotatedText = new Bitmap((int)(Math.Round(NewWidth)), (int)(Math.Round(NewHeight)));

        //Get graphic object of new isntantiated image for painting
        Graphics TextGraphic = Graphics.FromImage(RotatedText);
        TextGraphic.InterpolationMode = InterpolationMode.High;

        //Calcaute coordination of center of the image
        float OX = (float)NewWidth  / 2f;
        float OY = (float)NewHeight / 2f;

        //Apply transformations (translation, rotation, reverse translation)
        TextGraphic.TranslateTransform(OX, OY);
        TextGraphic.RotateTransform(Angle);
        TextGraphic.TranslateTransform(-OX, -OY);

        //Calculate the loaction of drawing text
        float X = (RotatedText.Width  - TextSize.Width ) / 2f;
        float Y = (RotatedText.Height - TextSize.Height) / 2f;

        //Draw the string
        TextGraphic.DrawString(Text, TextFont, Brushes.White, X, Y);

        //Return the image of rotated text
        return RotatedText;

    }

The result is this:

enter image description here

moorara
  • 3,897
  • 10
  • 47
  • 60
  • //Calculate size of the rotated text double NewWidth = Math.Abs(TextSize.Width * Math.Cos(AngleInRadian)) + Math.Abs(TextSize.Height * Math.Sin(AngleInRadian)); double NewHeight = Math.Abs(TextSize.Width * Math.Sin(AngleInRadian)) + Math.Abs(TextSize.Height * Math.Cos(AngleInRadian)); Is this calculation in your code correct ? – Kira Nov 14 '14 at 05:47
  • Should it be like //Calculate size of the rotated text double NewWidth = Math.Abs(TextSize.Width * Math.Cos(AngleInRadian)); double NewHeight = Math.Abs(TextSize.Width * Math.Sin(AngleInRadian)); – Kira Nov 14 '14 at 05:49
  • Now, I'm looking for calculating the exact width and height of rotated text. I checked the following link for calculation http://www.mathsisfun.com/algebra/trig-finding-side-right-triangle.html Checking the above link shows formula in my 2nd comment. But most of the developers are using your code (in my first comment). So, I'm just curious to know which one is correct. – Kira Nov 14 '14 at 05:55

2 Answers2

5

Try setting the TextRenderingHint property of your Graphics object to AntiAlias

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.textrenderinghint.aspx

Anders Marzi Tornblad
  • 18,896
  • 9
  • 51
  • 66
  • This won't work when capturing to bitmap and rotating. There is no hinting when rendering a bitmap at an angle – parapura rajkumar Dec 19 '11 at 11:50
  • 1
    Actually, that is not what is happening here. If you follow the code, you can tell that the `RotatedText` bitmap actually holds a prerotated text. The bitmap will not get rotated - the text is already rotated on the bitmap. So TextRendering will make a difference. Also, if the case were as you say, the results would not look like the resulting picture. – Anders Marzi Tornblad Dec 19 '11 at 11:57
  • 1
    Thanks Anders, that works well, using TranslateTransform. – Jack Apr 19 '20 at 13:56
-1

You can try a few things:

  • In your XAML:

    TextOptions.TextFormattingMode="Display" (and other TextOptions attached properties)

  • In your C#:

    TextOptions.SetTextFormattingMode(this, TextFormattingMode.Display);

  • Take a look at this SO topic

Community
  • 1
  • 1
Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88