Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Rotate Text Easily With GDI+
Drawing rotated text was a painful endeavor using the old Windows Graphics Device Interface. In .NET, it's simple.
by Bill Wagner

Posted October 31, 2002

Figure 1. Rotate Text the .NET Way

Remember how tiresome it was to format and draw rotated text for native Windows programs using GDI, the old graphics device API? First you had to make a rotated font to do it—and then it worked correctly only on some platforms. It got even worse if you wanted to center text because the function that told you the size of a displayed piece of text did not work on rotated fonts. You had to create the text at a zero-degree rotation, measure its size, and then create a new rotated font and draw the string.

GDI+ simplifies this process greatly. In Microsoft's new graphics device interface implementation, the company creates a consistent graphics model whose services are exposed through a set of C++ classes.

I've written a simple sample that shows you how easy it is to rotate text. My sample draws a text string in the middle of a window, rotated at any angle you want. Two features in GDI+ make this easy: graphics translations and graphics rotations.

To center the text, translate the coordinates so that the origin is in the center of the window:

Size sz = MyWindow.Size;
Point Middle = new Point (sz.Width / 2, 
   sz.Height / 2);
e.Graphics.TranslateTransform 
   (Middle.X, Middle.Y);

Rotate the text simply by applying another transform:

e.Graphics.RotateTransform (_angle);

Once the transforms are in place, just draw your text:

StringFormat format = new StringFormat 
   (StringFormatFlags.NoClip);
format.Alignment = 
   StringAlignment.Center;
format.LineAlignment = 
   StringAlignment.Center;
e.Graphics.DrawString 
   ("A simple TextString ", f, 
   Brushes.Black, 0, 0, format);

The string format objects control the alignment for the string. In this case, I chose the center of the string.

Now you can rotate your text at any angle you want. If you play with the sample, you'll notice that a positive angle, say 45 degrees, seems to rotate the text by –45 degrees (see Figure 1). Remember the standard coordinate system for GDI: y increases downward. With that kind of coordinate system, positive angles of rotation are clockwise from the horizontal axis.

This sample shows you one way that .NET's GDI+ API makes graphics programs simpler and more consistent. Download the sample code and modify the angles for the transformations. Notice that you can draw text at any angle, on any platform. And this works with any other shape rotated at any angle.

About the Author
Bill Wagner is SRT Solutions' Windows technology expert. He is a contributing editor for Visual Studio Magazine and the author of C# Core Language Little Black Book, an advanced reference for C# developers. Bill has had a lead design role in many projects in his 16 years of software development. He has designed software for engineering and business applications, for desktop and Web environments. He has an extensive background in 2-D and 3-D graphics and multimedia software, including developing the video playback engine used for "The Lion King Animated Storybook." Reach him at .

Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home