Draw transformed text in C#

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.TranslateTransform(80, 20, MatrixOrder.Append);
    e.Graphics.RotateTransform(20, 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);
        e.Graphics.DrawEllipse(Pens.Green,
            text_size.Width - 3, text_size.Height - 3, 6, 6);

        // Draw the text.
        e.Graphics.TextRenderingHint =
            System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
        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, translate it by 80 pixels horizontally and 20 pixels vertically, and then rotate it 20 degrees. 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 the 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.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.