Draw transformed text in C#

[transformed text]

This example uses the following Paint event handler to draw transformed text.

// Draw some transformed text.
private void Form1_Paint(object sender, PaintEventArgs e)
    // Transform.
    e.Graphics.ScaleTransform(1.5f, 1.5f, MatrixOrder.Append);
    e.Graphics.RotateTransform(25, MatrixOrder.Append);
    e.Graphics.TranslateTransform(80, 30, MatrixOrder.Append);

    // Make a font.
    using (Font the_font = new Font("Times New Roman", 20,
        FontStyle.Regular, GraphicsUnit.Pixel))
        // See how big the text will be when drawn.
        string the_text = "WYSIWYG";
        SizeF text_size = e.Graphics.MeasureString(the_text, the_font);

        // Draw a rectangle and two ellipses.
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.DrawRectangle(Pens.Blue, 0, 0,
            text_size.Width, text_size.Height);
        e.Graphics.DrawEllipse(Pens.Red, -3, -3, 6, 6);
            text_size.Width - 3, text_size.Height - 3, 6, 6);

        // Draw the text.
        e.Graphics.TextRenderingHint =
        e.Graphics.DrawString(the_text, the_font,
            Brushes.Brown, 0, 0);

The code adds transformations to the Graphics object to scale the text by a factor of 1.5 in the X and Y directions, rotate it 25 degrees, and then translate it by 80 pixels horizontally and 30 pixels vertically.

Note the final parameter MatrixOrder.Append passed to the transformation methods. By default new transformations are applied before existing ones (which seems very backwards to me) so you need to add this parameter if you want the transformations applied in the natural order.

In general you get different results if you change the order of a set of transformations. Try it and see.

The program creates the font it will use to draw the text and then uses the Graphics object’s MeasureString method to see approximately how big the text will be when drawn with that font.

Next the program sets the Graphics object’s SmoothingMode property to produce smooth shapes and draws a rectangle and two ellipses. It draws a rectangle big enough to hold the text and ellipses at the rectangle’s upper left and lower right corners. The Graphics object’s transformations scale, translate, and rotate the rectangle and ellipses.

The program then sets the Graphics object’s TextRenderingHint to produce smooth text and draws the text at the origin. Again the transformations modify the result so the text appears inside the rectangle drawn earlier.

By using the Graphics object’s transformation methods, you can scale, rotate, translate, and even skew text, rectangles, and other graphics.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in drawing, fonts, graphics and tagged , , , , , , , , , . Bookmark the permalink.

2 Responses to Draw transformed text in C#

  1. Anonymous says:

    The Download link seems to be broken.

  2. Rod Stephens says:

    Sorry about that. I’ve fixed it. Thanks for pointing this out!

Comments are closed.