Draw Vertical String Text


   



  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      String s = "Accrington Stanley";
      StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical);
      Font f = new Font("Times New Roman", 14);

      SizeF sizef = g.MeasureString(s, f, Int32.MaxValue, sf);

      RectangleF rf = new RectangleF(20, 20, sizef.Width, sizef.Height);
      g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);
      g.DrawString(s, f, Brushes.Black, rf, sf);

      f.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


Text string's containing rectangle


   


  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      String s = "Accrington Stanley";
      StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical);
      Font f = new Font("Times New Roman", 14);

      SizeF sizef = g.MeasureString(s, f, Int32.MaxValue, sf);

      RectangleF rf = new RectangleF(20, 20, sizef.Width, sizef.Height);
      g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);
      g.DrawString(s, f, Brushes.Black, rf, sf);

      f.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Center each line of text horizontally and vertically


   



  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      

      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      String s = "This is a long string that will wrap. ";
      s += "It will be centered both vertically and horizontally.";
      Font f = new Font("Arial", 12);
      StringFormat sf = new StringFormat();

      sf.Alignment = StringAlignment.Center;        // horizontal alignment
      sf.LineAlignment = StringAlignment.Center;    // vertical alignment

      Rectangle r = new Rectangle(10, 10, 300, f.Height * 4);
      g.DrawRectangle(Pens.Black, r);
      g.DrawString(s, f, Brushes.Black, r, sf);
      f.Dispose();

    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


Draw string with hatch brush

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    Font f = new Font("Times New Roman", 48, FontStyle.Bold);
    HatchBrush hb = new HatchBrush(HatchStyle.Cross,Color.White, Color.Black);
    g.DrawString("Crazy Crosshatch", f, hb, 0, 0);
    f.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


DrawString with solid brush and rectangle

   
 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class HuckleberryFinnHalfHeight: Form
{
     public static void Main()
     {
          Application.Run(new HuckleberryFinnHalfHeight());
     }
     public HuckleberryFinnHalfHeight()
     {
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics  grfx = pea.Graphics;
          int       cx   = ClientSize.Width;
          int       cy   = ClientSize.Height;
          Pen       pen  = new Pen(ForeColor);
          Rectangle rect = new Rectangle(0, 0, cx / 2, cy / 2);
   
          grfx.DrawString("some", Font, new SolidBrush(ForeColor), rect);
   
          grfx.DrawLine(pen, 0,      cy / 2, cx / 2, cy / 2);
          grfx.DrawLine(pen, cx / 2, 0,      cx / 2, cy / 2);
     }
}

    


DrawString with solid brush, string format

   
 

using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
   
class BoldAndItalicTighter: Form
{
     public static void Main()
     {
          Application.Run(new BoldAndItalicTighter());
     }
     public BoldAndItalicTighter()
     {
          Text = "Bold and Italic (Tighter)";
          Font = new Font("Times New Roman", 24);
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }     
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          string str        = "text.";
          Brush        brush       = new SolidBrush(clr);
          Font         fontRegular = Font;
          Font         fontBold    = new Font(fontRegular, FontStyle.Bold);
          Font         fontItalic  = new Font(fontRegular, FontStyle.Italic);
          PointF       ptf         = new PointF(0, 0);
          StringFormat strfmt      = StringFormat.GenericTypographic;
          strfmt.FormatFlags      |= StringFormatFlags.MeasureTrailingSpaces;
   
          grfx.DrawString(str, fontRegular, brush, ptf, strfmt);
     }
}

    


TextRenderingHint: Anti-Aliased Text

using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;

class AntiAliasedText: Form
{
public static void Main()
{
Application.Run(new AntiAliasedText());
}
public AntiAliasedText()
{
Font = new Font(“Times New Roman”, 12);
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Brush brush = new SolidBrush(clr);
string str = “A “;
int cxText = (int) grfx.MeasureString(str, Font).Width;

for (int i = 0; i < 6; i++) { grfx.TextRenderingHint = (TextRenderingHint)i; grfx.DrawString(str, Font, brush, i * cxText, 0); } } } [/csharp]