Title: Draw backwards text in C#
This example draws backwards 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 property. The value AntiAliasGridFit makes drawn text come out smoothly anti-aliased so it doesn't have jagged edges.
The program then 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 instead of to the right.
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 its SizeMode property to AutoSize at design time.
Download the example to experiment with it and to see additional details.
|