Code: Drawing Vertical Text on a Form (Visual C#)
The document is archived and information here might be outdated

Code: Drawing Vertical Text on a Form (Visual C#)

Visual Studio .NET 2003

This example demonstrates drawing text on a form in a vertical orientation.


private void DrawVerticalText()
   System.Drawing.Graphics formGraphics = this.CreateGraphics();
   string drawString = "Sample Text";
   System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
   System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
   float x = 150.0f;
   float y = 50.0f;
   System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);
   formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);

Compiling the Code

This example requires:

  • A Windows Application project.

The code needs to be within the scope of the form class. The instance of the form is represented by this.

Robust Programming

You should always call Dispose on any objects that consume system resources, such as Font and Graphics objects.

The following conditions may cause an exception:

  • The Arial font is not installed.

See Also

Graphics Programming Example Topics | Graphics.DrawString Method | StringFormat.FormatFlags Property | StringFormatFlags Enumeration | Drawing Text with GDI+

© 2016 Microsoft