Search code examples
c#.netwinformstextsystem.drawing

Drawing vertically stacked text in WinForms


Preferably using a Graphics object, how do you draw a string so that the characters are still oriented normally, but are stacked vertically?


Solution

  • Just split the string into characters and draw each one adding the line height of your font to your Y-axis variable:

        protected override void OnPaint(PaintEventArgs e)
        {
            float x = 10.0F;
            float y = 10.0F;
    
            string drawString = "123";
    
            using(SolidBrush brush = new SolidBrush(Color.Black))
            using (Font drawFont = new Font("Arial", 16))
            {
                foreach (char c in drawString.ToCharArray())
                {
                    PointF p = new PointF(x, y);
                    e.Graphics.DrawString(c.ToString(), drawFont, brush, p);
    
                    y += drawFont.Height;
                }
            }
            base.OnPaint(e);
        }