Why doesn't TextRenderer.MeasureText measure text correctly using large fonts in C #?

What I am doing is getting the pixel size of the string and converting it to hundredths of an inch (i.e. pixels / DPI = inches, inches * 100 = hundredths of an inch). Here is my code:

private static SizeF TextSize(string text, Font txtFnt)
{
    SizeF txtSize = new SizeF();

    // The size returned is 'Size(int width, int height)' where width and height
    // are the dimensions of the string in pixels
    Size s = System.Windows.Forms.TextRenderer.MeasureText(text, txtFnt);

    // Value based on normal DPI settings of 96
    txtSize.Width = (float)Math.Ceiling((float)s.Width / 96f * 100f); 
    txtSize.Height = (float)Math.Ceiling((float)s.Height / 96f * 100f);

    return txtSize;
}

Now, using the Arial font, all this works fine for fonts smaller than 12, but after that the characters begin to be cropped because the calculated size is smaller than the actual size. I know that my DPI settings are set to 96. My fonts are all the same with changing the font size:

Font myFont = new Font("Arial", <font size>, FontStyle.Regular, GraphicsUnit.Point);

I believe that I need to use GraphicsUnit.Pointbecause of the user control into which I draw the lines, but does it matter GraphicsUnit?

Is the function MeasureTexteven correct, or is something else going on?

. "Inches/100" (, ). , , .. "", .

+5
3

, , INSIDE. , . Win32 API System.Drawing. , Win32 API, Win32 API. , System.Drawing, .net .

Forms Win32 , Win32.

, , , .

+3

, - . , - . ( , J #):

private static System.Drawing.StringFormat createStringFormat()
{

    System.Drawing.StringFormat fmt = new System.Drawing.StringFormat (System.Drawing.StringFormat.get_GenericTypographic());
    fmt.set_Trimming(System.Drawing.StringTrimming.None);
    fmt.set_FormatFlags(System.Drawing.StringFormatFlags.MeasureTrailingSpaces | System.Drawing.StringFormatFlags.NoWrap);
    return fmt;
}
0

Font.

The fonts used by Windows are hinted, which means that they cannot be exactly what the font indicates.

http://blogs.msdn.com/b/cjacks/archive/2006/05/11/595525.aspx

0
source

All Articles