Home > Articles > Programming > .NET and Windows Programming

Using Geometric Transforms for Text Effects in .NET

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close Window

Peter Aitken

Learn more…

Debugging 101 for Visual Studio
Sep 15, 2006
Fun with Fractals in Visual Studio .NET
Aug 11, 2006
Exploring Excel Functions, Part 9: Analyzing Frequency Distributions
Aug 4, 2006
File Management in .NET
Jul 7, 2006
Exploring Excel Functions 8: Predicting the Future
Jun 30, 2006
Exploring Excel's Functions, Part 7: Rounding with Excel
Jun 23, 2006
Binary File Access in the .NET Framework
Jun 16, 2006
Text File Access in the .NET Framework
Jun 2, 2006
Using Geometric Transforms for Text Effects in .NET
May 26, 2006
Understanding MDI Applications in .NET
May 12, 2006
A .NET Framework Text and Font Primer
Apr 21, 2006
Exploring Excel's Functions, Part 6: TTEST() Function
Apr 14, 2006
Troubleshooting Excel PivotTables
Apr 7, 2006
Troubleshooting Word Tables
Mar 31, 2006
Exploring Excel's Functions, Part 5: The Power of Choice
Mar 24, 2006
Seven Things I Hate About Word Printing
Mar 10, 2006
Exploring Excel's Functions, Part 4: Database Functions
Feb 24, 2006
Exploring Excel's Functions Part 3: CELL() Shocked
Feb 3, 2006
Exploring Excel's Functions Part 2: ADDRESS() and INDIRECT()
Dec 30, 2005
Exploring Excel's Functions: IF() Only!
Dec 9, 2005
Advanced Find-and-Replace Tools in Word
Oct 28, 2005
Going Beyond Basic Spaces and Hyphens in Word
Sep 16, 2005
Get Organized with Word's Outline Tools
Aug 19, 2005
Managing Your Money in Microsoft Excel: Basic Financial Calculations
Jul 29, 2005
Structured Exception Handling in Visual Studio .NET
Oct 8, 2004
Multithreading with the .NET Framework
Jun 11, 2004
.NET Tools for Working with XML
May 21, 2004
Storing Information: Variables and Constants in C
Mar 28, 2003
Introducing Web Programming with .NET
Mar 1, 2002
Using Web Forms
Mar 1, 2002
Introducing Web Services
Feb 15, 2002
XML and the .NET Framework
Feb 8, 2002
ASP.NET Programming: Using Web Forms
Jan 25, 2002
Understanding the Common Features of Web Controls
Aug 20, 2001

Sorry, this author hasn't posted any blogs.

Peter Aitken points out that text is just a graphic image and therefore can be manipulated like any other graphic. .NET provides some built-in tools for manipulating the size, shape, and orientation of text. Just be sure it remains readable!

Geometric transforms let you take an image and distort it in some way, such as rotation, translation, flipping, or stretching. Geometric transforms are a rather complex topic and are well beyond explaining in a single article. But you can learn and use one very handy aspect of transforms without a lot of fuss: creating special text effects. After all, text is just a graphic image of letters and other characters, so transforming the images geometrically should—and does—work perfectly well.

How can you transform text? The possibilities fall into four main categories:

  • Scaling. Stretch or shrink the text in the vertical and/or horizontal direction.
  • Reflection. Create a mirror image of text.
  • Shearing. Tilt and slant the text in various ways.
  • Rotation. Display text at any desired angle.

Now let’s see how to create these transforms.

Transforms and Matrices

In .NET, geometric transforms are represented by matrixes. You may remember from math class that a matrix is a grid of numbers. But even if you don’t know a matrix from a mango, you can still work with many transforms. It’s true that for some of the more complex transforms you must create the transformation matrix yourself, in code, but for the commonly needed transforms the required tools are built in. A transformation matrix is still at work behind the scenes, but the .NET classes take care of the details for you.

Scale Transforms

A scale transform changes the size of a graphic in the x and/or y direction. Figure 1 shows untransformed text (used in all the examples in this article), and Figure 2 shows text that has been scaled by a factor of 2 in the y direction.

Figure 1

Figure 1 Untransformed text used in the examples.

Figure 2

Figure 2 Text scaled by a factor of 2 in the y direction.

To apply a scale transform, you call the ScaleTransform() method of the Graphics object that will be used to do the drawing. In this and other code examples assume that g is a reference to your Graphics object. Here’s the syntax:

g.ScaleTransform(Xscale, Yscale)

The two arguments, both of type float, specify the scaling in the x and y directions; pass a value of 1 for no scaling. The following example stretches the graphic to twice its normal width while leaving the height unchanged:

g.ScaleTransform(2.0F, 1.0F)

Reflection

Reflecting a graphic also makes use of the ScaleTransform() method. By passing a negative argument for Yscale, you reflect about the x axis (flip vertically); by passing a negative argument for Xscale, you reflect about the y axis (flip horizontally). The following code causes a reflection about the x axis, as shown in Figure 3.

g.ScaleTransform(1.0F, -1.0F)
Figure 3

Figure 3 Text reflected about the x axis.

Shearing

Shearing tilts the text in one direction or another. Figure 4 shows text that has been sheared to the left.

Figure 4

Figure 4 Text sheared to the left.

Shearing is a bit more complicated than the other transforms we’ve examined so far, because it requires that you create a matrix for the transform. Fortunately, the Matrix class offers the Shear() method to do the work for you. These are the steps:

  1. Create an instance of the Matrix class.
  2. Call its Shear() method to define the desired shear amount and direction.
  3. Assign the Matrix object to the Transform property of your Graphics object.

The Shear method uses this syntax:

Shear(Xshear, Yshear)

A value of 0 specifies no shear in that direction. To shear horizontally to the left or right, pass a positive or negative Xshear value. Here’s the relevant code snippet for the transform shown in Figure 4.

Matrix m = new Matrix();
m.Shear(0.5F, 0);
g.Transform = m;

To shear vertically down or up, pass a positive or negative Yshear value. Vertical shearing works as shown in Figure 5. At first glance, this example looks like a rotation, but, unlike true rotation (covered in the next section), the individual letters remain upright with vertical shearing.

Figure 5

Figure 5 Text sheared vertically.

Rotation

Rotation is another transform that can be done with a Graphics class method, specifically RotateTransform(). Pass an argument specifying the rotation in degrees; positive values rotate clockwise and negative values rotate counterclockwise. Figure 6 shows the result of the following transform:

g.RotateTransform(45);
Figure 6

Figure 6 Text rotated 45 degrees clockwise.

Transforms and Output Coordinates

Transforms work behind the scenes by changing the x and y coordinates of the individual pixels in the image. Unavoidably, some transforms change not only the appearance of the text but its output location. You’ll see this principle demonstrated in the program in the following section; please examine the code to see how it accounts for these location changes. You can also experiment on your own to get a feel for how the various transforms change output location.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on November 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network