I am having a label whose Font definition is like shown below
label2.Font =
new System.Drawing.Font("Microsoft Sans Serif", 11f, System.Drawing.GraphicsUnit.Pixel);
There is a discrepancy between the size of the text shown by this label when it is viewed in 96 DPI and 120 DPI, in 120 DPI the height and width of the font is bigger compared to what is shown in 96 DPI.
As my font definition is in Pixels rather in Points, I expect the text shown in the label should be same in both 96 DPI and 120 DPI, but instead the text shown in 120 DPI is bigger.
Please help me to know the reason.Thanks in advance. - Edited byhikanagu Friday, June 05, 2009 12:56 PM
-
| | hikanagu Friday, May 29, 2009 11:27 AM |
Hi hikanagu,
In your last reply, you want to know why there is a display difference of the text when you are using the samefont sizes (expressed in pixels)between 96 DPI and 120 DPI. I can give you a simple explanation below:
DPI means dots per inch. The actual size you can see is in points (1/72 inch), but you used pixels to measure the font, so the actual size will become smaller as DPI increases. The relation between points and pixels is below: sizeInPixels = (sizeInPoints * dpi)/72 .
I suggest using points instead of pixels, like the codebelow:
float fontSizeInPixelsInDPI96 = 16f; // the font size in pixels
float newFontSizeInPoints = fontSizeInPixelsInDPI96 * 72f / 96; // calculate the size in points
//change the font using the new size in points
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif",
newFontSizeInPoints, System.Drawing.GraphicsUnit.Point);
You can find detail information in Chapter 16 of the book Addison Wesley Windows Forms 2.0 Programming, which is written by ChrisSells, MichaelWeinhardt.
Below isan extract from the book whichdiscusses about the font size:
A font is an instance of the System.Drawing.Font class, which for each font includes a family, a style, and a size. And, as you might expect, a font family is an instance of the FontFamily class, which encapsulates a group of typefaces that differ only in style. A typeface is a named collection of drawing strokes that make up the outlines of the characters, such as those you're reading right now. It's the typeface name that you're used to seeing in the "Font" menu of most programs. The font style constitutes the variations within a typeface, such as bold, italics, underline, and size. So, a typeface would be Arial, a font family would include Arial Regular and Arial Bold, and a font would be 12-point Arial Bold.
Fonts can be measured in several sizes other than points, including pixels, ems, and design units. A pixel is a point of light on a screen or a point of ink on a printer. Pixels are often packed into inches for measurement. For example, the resolution of video display adapters and printers is typically specified in dots per inch (dpi), where a dot is the same as a pixel. Pixels are device-dependent, so a pixel on a 72-dpi display bears no size relationship to a pixel on a 300-dpi printer.
A point, on the other hand, is inch no matter what device it's drawn on, and the Graphics object scales appropriately as text is drawn. If you want to ensure that a font is rendered to the target device in the correct size, you need to convert between points and pixels. This requires knowing the dpi of the device you're drawing on, which is conveniently available via the Graphics.DpiY property:[1]
[1] There's also a Graphics.DpiX property, but that's used for measuring width and is not useful as related to font height.
using( Graphics g = this.CreateGraphics() ) {
// A 12-point font is 16 pixels high on a 96-dpi monitor
float dpi = g.DpiY;
float points = 12f;
float pixels = (points * dpi)/72f; //=16f
...
}
The em unit of measure is so named because metal typesetters used uppercase M as the guide against which all other letters were measured. M was used because it took up the most vertical and horizontal space. Consequently, the number of points specified for a font represents "one em" for that font.
Finally, design units are a font designer's way to specify a font family's dimensions regardless of the resolution of the rendering device or the size of the rendered font. For example, Arial has a height of 2,048 design units. The design units are used to scale a font family to a point size when individual strokes of the font are rendered (more on this later).
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. - Edited byAland LiMSFT, ModeratorTuesday, June 02, 2009 3:11 AM
- Proposed As Answer byAland LiMSFT, ModeratorWednesday, June 03, 2009 2:36 AM
- Marked As Answer byAland LiMSFT, ModeratorThursday, June 04, 2009 11:37 AM
- Edited byAland LiMSFT, ModeratorTuesday, June 02, 2009 7:46 AM
-
| | Aland Li Tuesday, June 02, 2009 3:09 AM | Hi hikanagu,
From your description, you need to make sure that the size of the font looks the same in different DPI setting.
When the DPI setting is changed, I think you need to recalculate the font size. Below is the sample code:
/// <summary>
/// Recalculatew the font size in pixels
/// </summary>
/// <param name="pixelsDPI96">old size when DPI is 96</param>
/// <param name="dpi">new DPI</param>
/// <returns></returns>
private float GetNewPixels(float pixelsDPI96, float dpi)
{
return pixelsDPI96 * 96 / dpi;
}
/// <summary>
/// An example showing how to use GetNewPixels method
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
using (Graphics g = this.CreateGraphics())
{
float dpi = g.DpiY;
float newFontSize = GetNewPixels(16f, dpi);
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 16, System.Drawing.GraphicsUnit.Pixel);
}
}
Let me know if this helps.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. | | Aland Li Monday, June 01, 2009 9:40 AM | Hi Aland,
Thanks very much for your timelyreply. But unfortunately, this doesn't solve my issue. It is shrinking the font size too small.For the font size of 16px, the calculation as per your solution would be,
For 96DPI
newFontSize = 16 * 96/96 = 16F.
But for 120 DPI
newFontSize = 16 * 96/120 = 12.8F.
From the second calculation you could find that this is shrinking the font size too small. According to my understanding I think that we should not do any calculation for resizing or scaling if we are using fixed pixels for specifying the size(as sizes specified in pixels are independent of user's DPI setting), Correct me If I am wrong.
As this is the case, I am curious to know why there is a difference indisplay of the text which is using samefont sizes (expressed in pixels)between 96 DPI and 120 DPI . | | hikanagu Monday, June 01, 2009 1:42 PM |
Hi hikanagu,
In your last reply, you want to know why there is a display difference of the text when you are using the samefont sizes (expressed in pixels)between 96 DPI and 120 DPI. I can give you a simple explanation below:
DPI means dots per inch. The actual size you can see is in points (1/72 inch), but you used pixels to measure the font, so the actual size will become smaller as DPI increases. The relation between points and pixels is below: sizeInPixels = (sizeInPoints * dpi)/72 .
I suggest using points instead of pixels, like the codebelow:
float fontSizeInPixelsInDPI96 = 16f; // the font size in pixels
float newFontSizeInPoints = fontSizeInPixelsInDPI96 * 72f / 96; // calculate the size in points
//change the font using the new size in points
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif",
newFontSizeInPoints, System.Drawing.GraphicsUnit.Point);
You can find detail information in Chapter 16 of the book Addison Wesley Windows Forms 2.0 Programming, which is written by ChrisSells, MichaelWeinhardt.
Below isan extract from the book whichdiscusses about the font size:
A font is an instance of the System.Drawing.Font class, which for each font includes a family, a style, and a size. And, as you might expect, a font family is an instance of the FontFamily class, which encapsulates a group of typefaces that differ only in style. A typeface is a named collection of drawing strokes that make up the outlines of the characters, such as those you're reading right now. It's the typeface name that you're used to seeing in the "Font" menu of most programs. The font style constitutes the variations within a typeface, such as bold, italics, underline, and size. So, a typeface would be Arial, a font family would include Arial Regular and Arial Bold, and a font would be 12-point Arial Bold.
Fonts can be measured in several sizes other than points, including pixels, ems, and design units. A pixel is a point of light on a screen or a point of ink on a printer. Pixels are often packed into inches for measurement. For example, the resolution of video display adapters and printers is typically specified in dots per inch (dpi), where a dot is the same as a pixel. Pixels are device-dependent, so a pixel on a 72-dpi display bears no size relationship to a pixel on a 300-dpi printer.
A point, on the other hand, is inch no matter what device it's drawn on, and the Graphics object scales appropriately as text is drawn. If you want to ensure that a font is rendered to the target device in the correct size, you need to convert between points and pixels. This requires knowing the dpi of the device you're drawing on, which is conveniently available via the Graphics.DpiY property:[1]
[1] There's also a Graphics.DpiX property, but that's used for measuring width and is not useful as related to font height.
using( Graphics g = this.CreateGraphics() ) {
// A 12-point font is 16 pixels high on a 96-dpi monitor
float dpi = g.DpiY;
float points = 12f;
float pixels = (points * dpi)/72f; //=16f
...
}
The em unit of measure is so named because metal typesetters used uppercase M as the guide against which all other letters were measured. M was used because it took up the most vertical and horizontal space. Consequently, the number of points specified for a font represents "one em" for that font.
Finally, design units are a font designer's way to specify a font family's dimensions regardless of the resolution of the rendering device or the size of the rendered font. For example, Arial has a height of 2,048 design units. The design units are used to scale a font family to a point size when individual strokes of the font are rendered (more on this later).
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. - Edited byAland LiMSFT, ModeratorTuesday, June 02, 2009 3:11 AM
- Proposed As Answer byAland LiMSFT, ModeratorWednesday, June 03, 2009 2:36 AM
- Marked As Answer byAland LiMSFT, ModeratorThursday, June 04, 2009 11:37 AM
- Edited byAland LiMSFT, ModeratorTuesday, June 02, 2009 7:46 AM
-
| | Aland Li Tuesday, June 02, 2009 3:09 AM |
|