Font.Size returns incorrect size?

  • Thread starter Thread starter Amit
  • Start date Start date
A

Amit

Hello,
I'm using the FontDialog to select a font and its size. When I return from
the dialog, the size returned by fontDialog.Font.Size is always a bit off
from the size I selected. For example when I select size 11, the size
returned by the dialog is 11.25, when I select size 10 the returned size is
9.75. I can work around it by simply rounding the value returned by
Font.Size but does anyone know why it does this? I've set the units to
Points.
Thanks
Amit
 
I'm using the FontDialog to select a font and its size. When I return from
the dialog, the size returned by fontDialog.Font.Size is always a bit off
from the size I selected. For example when I select size 11, the size
returned by the dialog is 11.25, when I select size 10 the returned size is
9.75. I can work around it by simply rounding the value returned by
Font.Size but does anyone know why it does this? I've set the units to
Points.

For any Font object, the SizeInPoints value is the Size in terms of
the font's current Unit which depends on the display.

When you request a 12 pt font you should get a SizeInPoints that is
indeed 12.00. But since the font is displayed on a low-resolution
raster display with a Unit of GraphicsUnit.Pixel, the actual Size is
somewhat off, depending on how the font is rasterized.

For precise display calculations, you need to use Size without
rounding. For showing a font's point size, use SizeInPoints instead.

See Charles Petzold, "Programming Microsoft Windows with C#",
Chapter 9: Text and Fonts.
 
For precise display calculations, you need to use Size without
rounding.

Correction: it's probably better to use Font.Height which already
gives the correctly rounded line height in pixels.
 
This is because you've set the font height in pixels measured at 96 DPI and
then read the height in points.

Declare the font explicitly with the GraphicsUnit.Points setting.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Christoph Nahr said:
Correction: it's probably better to use Font.Height which already
gives the correctly rounded line height in pixels.

Thanks, this looks like the easiest way to get the right height.
 
Back
Top