Draw font cell ascent, cell descent, line space, em height

image_pdfimage_print
   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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);
    FontFamily ff = new FontFamily("Times New Roman");
    float emSizeInGU = 24f;
      Font f = new Font(ff, emSizeInGU);

    int emSizeInDU = ff.GetEmHeight(FontStyle.Regular);
    int ascentInDU = ff.GetCellAscent(FontStyle.Regular);
    int descentInDU = ff.GetCellDescent(FontStyle.Regular);
    int lineSpacingInDU = ff.GetLineSpacing(FontStyle.Regular);

    float ascentInGU = ascentInDU * (emSizeInGU / emSizeInDU);
    float descentInGU = descentInDU * (emSizeInGU / emSizeInDU);
    float lineSpacingInGU = lineSpacingInDU * (emSizeInGU / emSizeInDU);

    PointF textOrigin = new PointF(20, 20);
    PointF nextLineOrigin = new PointF(textOrigin.X,textOrigin.Y + f.Height);
    g.DrawString("AxgQ", f, Brushes.Black, textOrigin);
    g.DrawString("AxgQ", f, Brushes.Black, nextLineOrigin);

    int lineLen = 100;
    g.DrawLine(Pens.Blue,textOrigin,new PointF(textOrigin.X + lineLen, textOrigin.Y));
    g.DrawLine(Pens.Red,nextLineOrigin,new PointF(nextLineOrigin.X + lineLen, nextLineOrigin.Y));

    PointF p = new PointF(textOrigin.X,textOrigin.Y + lineSpacingInGU);
    g.DrawLine(Pens.Blue, p,new PointF(p.X + lineLen, p.Y));

    p = new PointF(nextLineOrigin.X,nextLineOrigin.Y + lineSpacingInGU);
    g.DrawLine(Pens.Red, p,new PointF(p.X + lineLen, p.Y));

    p = new PointF(textOrigin.X,textOrigin.Y + lineSpacingInGU - ascentInGU);
    g.DrawLine(Pens.Blue, p,new PointF(p.X + lineLen, p.Y));

    p = new PointF(nextLineOrigin.X, nextLineOrigin.Y +lineSpacingInGU - ascentInGU);
    g.DrawLine(Pens.Red, p, new PointF(p.X + lineLen, p.Y));

    p = new PointF(textOrigin.X,textOrigin.Y + lineSpacingInGU + descentInGU);
    g.DrawLine(Pens.Blue, p,new PointF(p.X + lineLen, p.Y));

    p = new PointF(nextLineOrigin.X,nextLineOrigin.Y + lineSpacingInGU + descentInGU);
    g.DrawLine(Pens.Red, p,new PointF(p.X + lineLen, p.Y));
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


Get font cell ascent, descent, LineSpacing and EmHeight

image_pdfimage_print


   

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

  public class Test{
    static void Main() {
      FontFamily myFamily = new FontFamily("Verdana");
      Font myFont = new Font(myFamily, 12);
      int fontHeight = myFont.Height;

      Console.WriteLine("Measurements are in GraphicsUnit." + myFont.Unit.ToString());

      Console.WriteLine("The Verdana family.");

      // Print our Family ties...
      Console.WriteLine("Ascent for bold Verdana: " + myFamily.GetCellAscent(FontStyle.Bold));

      Console.WriteLine("Descent for bold Verdana: " + myFamily.GetCellDescent(FontStyle.Bold));

      Console.WriteLine("Line spacing for bold Verdana: " + myFamily.GetLineSpacing(FontStyle.Bold));

      Console.WriteLine("Height for bold Verdana: " + myFamily.GetEmHeight(FontStyle.Bold));
    }
  }

           
          


Font Style: Bold and Strikeout

image_pdfimage_print


   


  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 = "Pen Cap App";
      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);

      Font font= new Font("Times New Roman", 12, FontStyle.Bold | FontStyle.Strikeout);
      
      g.DrawString("Bold & Strikeout", font, Brushes.Black, 0, 0);

      font.Dispose();
    }

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


           
          


Font Style: Underline

image_pdfimage_print


   



  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 = "Pen Cap App";
      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);

      Font font = new Font("Times New Roman", 12, FontStyle.Underline);
      
      g.DrawString("Underline", font, Brushes.Black, 0, 0);

      font.Dispose();
    }

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

           
          


Font Style: Strike out

image_pdfimage_print


   


  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 = "Pen Cap App";
      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);

      Font font  = new Font("Times New Roman", 12, FontStyle.Strikeout);
      
      g.DrawString("Strikeout", font, Brushes.Black, 0, 0);

      font.Dispose();
    }

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


           
          


Font Style: Bold and Italic

image_pdfimage_print


   



  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 = "Pen Cap App";
      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);

      Font font = new Font("Times New Roman", 12, FontStyle.Bold | FontStyle.Italic);

      g.DrawString("Bold-Italic", font, Brushes.Black, 0, 0);

      font.Dispose();
    }

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

           
          


Font Style: Italic

image_pdfimage_print


   


  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 = "Pen Cap App";
      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);

      Font font = new Font("Times New Roman", 12, FontStyle.Italic);

      g.DrawString("Italic", font, Brushes.Black, 0, 0);

      font.Dispose();
    }

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