Align text drawn by DrawString in C#


This example shows how to use StringFormat objects and Rectangles to align text drawn in in C#.

This example’s Paint event handler draws nine strings inside a rectangle, aligning them vertically and horizontally in the nine possible combinations of top/left, top/center, bottom/right, and so forth.

The StringFormat object’s LineAlignment property determines how the text is aligned vertically. It can take the values Near (top), Center (middle), or Far (bottom).

The Alignment property determines how the text is aligned horizontally. It can take the same values Near (left), Center (middle), or Far (right).

(This is a good example of overusing a piece of code, in this case, an enumeration. Instead of creating separate enumerations Top/Center/Bottom and Left/Middle/Right, .NET uses the single StringAlignment enumeration to represent both. Mmicrosoft is trying to make a single enumeration serve two purposes and that makes it harder to figure out what the values mean. That and their names also makes it harder to remember which of Alignment and LineAlignment represents horizontal alignment and which represents vertical alignment. This would all be much more straightforward if Microsoft had used VerticalAlignment and HorizontalAlignment for the property names and separate enumerations for their values.)

The following code shows how the program draws its text aligned in the rectangle’s upper left corner.

// Draw text aligned in various ways.
private void Form1_Paint(object sender, PaintEventArgs e)
    Rectangle rect = new Rectangle(5, 5,
        ClientSize.Width - 10, ClientSize.Height - 10);
    e.Graphics.DrawRectangle(Pens.Red, rect);

    using (Font font = new Font("Times New Roman", 16,
        using (StringFormat sf = new StringFormat())
            // Top.
            sf.LineAlignment = StringAlignment.Near;

            // Top/Left.
            sf.Alignment = StringAlignment.Near;
            e.Graphics.DrawString("Top/Left", font,
                Brushes.Black, rect, sf);

This code creates a Rectangle slightly smaller than the form and draws it in red. It then creates a Font and a StringFormat object.

To draw the text at the top of the rectangle, the code sets the StringFormat object’s LineAlignment property to Near. To draw the text in the upper left corner, it also sets the StringFormat‘s Alignment property to Near.

It then uses the Graphics object’s DrawString method to draw the text “Top/Left” using the StringFormat object as a parameter.

The rest of the code is similar. It uses different Alignment and LineAlignment values to align text in various ways. Download the example to see the details.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in graphics, strings and tagged , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Align text drawn by DrawString in C#

  1. Jasonlhy says:

    It only applies if you need Layout Rectangle, if you use drawString with PointF, it near position is right to elft

    • RodStephens says:

      If you don’t have a layout rectangle then alignment is a bit different, but you can still use the StringFormat object to align the text. You can use it to make the text centered, left, right, above, or below a point.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.