Draw backwards text in C#

This example draws backward text in a PictureBox. You could use similar techniques to draw backward text on a form, printout, bitmap file, or other drawing surface.

When the program starts, the following code executes. It draws the text into a Bitmap and then displays the Bitmap in the PictureBox.

private void Form1_Load(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(280, 100);
using (Graphics gr = Graphics.FromImage(bm))
{
gr.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
gr.ScaleTransform(-1, 1);
using (Font the_font = new Font("Comic Sans MS", 40))
{
gr.DrawString("Backward", the_font, Brushes.Black, -280, 0);
picBackward.Image = bm;
}
}
}

The code first makes a Bitmap big enough to hold the text. It then creates a Graphics object associated with the Bitmap.

Next the program sets the Graphic object's TextRenderingHint. The value AntiAliasGridFit makes drawn text come out smoothly anti-aliased so it isn’t jagged.

The program next applies a scale transform to the Graphics object that scales all graphics by a factor of -1 horizontally, making the result appear backward.

The program then creates a Font and calls the Graphics object’s DrawString method to draw the text. The code starts the text at position (-280, 0). The scale transform reverses the X coordinates of the text so the word “Backward” actually starts at position (280, 0) and draws to the left.

The program finishes by displaying the Bitmap in the PictureBox.

Tip: To make the PictureBox display the Bitmap at its full size, I set the PictureBox's SizeMode property to AutoSize at design time.

   

 

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.