Darkpwny
Darkpwny

Reputation: 13

Windows C# Font Size calcs

I was wondering what was causing the following issue: If I create a new font, of a specified size in points, then measure a string - in points again, I get two different values?

i.e.

        Font font = new Font("Arial", 36, GraphicsUnit.Point);

        Graphics g =  CreateGraphics();

        g.PageUnit = GraphicsUnit.Point;

        string str = "hello";

        SizeF size = g.MeasureString(str, font);

size.height == 44 as opposed to 36 which is the specified PT size of the font!!

Whats going on??!!!

Upvotes: 1

Views: 845

Answers (2)

MusiGenesis
MusiGenesis

Reputation: 75296

MeasureString pads the returned rect to allow for overhanging and underhanging characters. This shows a typical rect returned by MeasureString:

enter image description here

So naturally the returned height will be larger than the specified size of the font in points.

Upvotes: 1

iraSenthil
iraSenthil

Reputation: 11577

Take a look at this link. Extra space might be the reason for different sizes

The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs

Upvotes: 0

Related Questions