C# vertical label in a Windows Forms

Is it possible to display a label vertically in a Windows Forms?


Labels are easy, all you have to do is override the Paint event and draw the text vertically. Do note that GDI is optimised for Drawing text horizontally. If you rotate text (even if you rotate through multiples of 90 degrees) it will looks notably worse.

Perhaps the best thing to do is draw your text (or get a label to draw itself) onto a bitmap, then display the bitmap rotated.

Some C# code for drawing a Custom Control with vertical text. Note that ClearType text NEVER works if the text is not horizontal:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;


public partial class VerticalLabel : UserControl
{
    public VerticalLabel()
    {
        InitializeComponent();
    }

    private void VerticalLabel_SizeChanged(object sender, EventArgs e)
    {
        GenerateTexture();
    }

    private void GenerateTexture()
    {
        StringFormat format = new StringFormat();
        format.Alignment = StringAlignment.Center;
        format.LineAlignment = StringAlignment.Center;
        format.Trimming = StringTrimming.EllipsisCharacter;

        Bitmap img = new Bitmap(this.Height, this.Width);
        Graphics G = Graphics.FromImage(img);

        G.Clear(this.BackColor);

        SolidBrush brush_text = new SolidBrush(this.ForeColor);
        G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
        G.DrawString(this.Name, this.Font, brush_text, new Rectangle(0, 0, img.Width, img.Height), format);
        brush_text.Dispose();

        img.RotateFlip(RotateFlipType.Rotate270FlipNone);

        this.BackgroundImage = img;
    }
}

Create a class myLabel which can rotate it's Text on any angle specified by you.

You can use it by code or simply dragging from ToolBox

using System.Drawing;

class myLabel:System.Windows.Forms.Label
{
    public int RotateAngle { get; set; }  // to rotate your text
    public string NewText { get; set; }   // to draw text
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        Brush b =new SolidBrush(this.ForeColor);           
        e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
        e.Graphics.RotateTransform(this.RotateAngle);
        e.Graphics.DrawString(this.NewText, this.Font,b , 0f, 0f);
        base.OnPaint(e);
    }
}

Now this custom control is used into your form.

You have to set below properties

 1. mylbl.Text = "";             //which can be changed by NewText property
 2. mylbl.AutoSize = false;      // adjust according to your text
 3. mylbl.NewText = "Hello";     // whatever you want to display
 4. mylbl.ForeColor = Color.Red;  // color to display
 5. mylbl.RotateAngle = -90;     //angle to rotate

I expanded on Javed Akram's answer to resize the widget automatically (I needed this feature). It works for both positive and negative angles, the way that Javed states:

 1. mylbl.Text = "";             // which can be changed by NewText property
 2. mylbl.AutoSize = false;      // adjust according to your text
 3. mylbl.NewText = "Hello";     // whatever you want to display
 4. mylbl.ForeColor = Color.Red; // color to display
 5. mylbl.RotateAngle = -90;     // angle to rotate

Here is the code:

public class RotatingLabel : System.Windows.Forms.Label
{
    private int m_RotateAngle = 0;
    private string m_NewText = string.Empty;

    public int RotateAngle { get { return m_RotateAngle; } set { m_RotateAngle = value; Invalidate(); } }
    public string NewText { get { return m_NewText; } set { m_NewText = value; Invalidate(); } }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        Func<double, double> DegToRad = (angle) => Math.PI * angle / 180.0;

        Brush b = new SolidBrush(this.ForeColor);
        SizeF size = e.Graphics.MeasureString(this.NewText, this.Font, this.Parent.Width);

        int normalAngle = ((RotateAngle % 360) + 360) % 360;
        double normaleRads = DegToRad(normalAngle);

        int hSinTheta = (int)Math.Ceiling((size.Height * Math.Sin(normaleRads)));
        int wCosTheta = (int)Math.Ceiling((size.Width * Math.Cos(normaleRads)));
        int wSinTheta = (int)Math.Ceiling((size.Width * Math.Sin(normaleRads)));
        int hCosTheta = (int)Math.Ceiling((size.Height * Math.Cos(normaleRads)));

        int rotatedWidth = Math.Abs(hSinTheta) + Math.Abs(wCosTheta);
        int rotatedHeight = Math.Abs(wSinTheta) + Math.Abs(hCosTheta);

        this.Width = rotatedWidth;
        this.Height = rotatedHeight;

        int numQuadrants = 
            (normalAngle >= 0 && normalAngle < 90) ? 1 :
            (normalAngle >= 90 && normalAngle < 180) ? 2 :
            (normalAngle >= 180 && normalAngle < 270) ? 3 :
            (normalAngle >= 270 && normalAngle < 360) ? 4 :
            0;

        int horizShift = 0;
        int vertShift = 0;

        if (numQuadrants == 1)
        {
            horizShift = Math.Abs(hSinTheta);
        }
        else if (numQuadrants == 2)
        {
            horizShift = rotatedWidth;
            vertShift = Math.Abs(hCosTheta);
        }
        else if (numQuadrants == 3)
        {
            horizShift = Math.Abs(wCosTheta);
            vertShift = rotatedHeight;
        }
        else if (numQuadrants == 4)
        {
            vertShift = Math.Abs(wSinTheta);
        }

        e.Graphics.TranslateTransform(horizShift, vertShift);
        e.Graphics.RotateTransform(this.RotateAngle);

        e.Graphics.DrawString(this.NewText, this.Font, b, 0f, 0f);
        base.OnPaint(e);
    }
}