Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotnet: How do you get average character width for a font?

Tags:

.net

fonts

Windows Forms:

For System.Drawing there is a way to get the font height.

Font font = new Font("Arial", 10 , FontStyle.Regular);
float fontHeight = font.GetHeight(); 

But how do you get the other text metrics like average character width?

like image 634
P a u l Avatar asked Nov 21 '08 13:11

P a u l


People also ask

What is the width of a font?

Typographers measure the cap height of a font in units called points. A point is 1/72nd of an inch. There are twelve points in a pica, and six picas equal one inch. The width of each character is called the set width.

How do I increase the width of a character in CSS?

What you're looking for is transform:scale(x, y) . For example: -webkit-transform:scale(2.0, 1.0); -moz-transform:scale(2.0, 1.0); -ms-transform:scale(2.0, 1.0); -o-transform:scale(2.0, 1.0); transform:scale(2.0,1.0);


1 Answers

Use Graphics.MeasureString Method

private void MeasureStringMin(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}
like image 126
Ramesh Soni Avatar answered Oct 22 '22 09:10

Ramesh Soni