.NET System.Drawing.Font - Get Available Sizes and Styles
Asked Answered
J

3

1

I have one combo that allows user to select a font name.

The 2nd is supposed to show available sizes of the font. The 3rd has to show available styles.

Question: how can I retrieve the sizes and styles selected System.Drawing.Font supports?

Junction answered 28/12, 2011 at 20:52 Comment(2)
You would have to go back to Windows version 3 to find fonts that are only available in certain sizes. Device fonts. TrueType fonts can be rendered in any size. And support synthesizing styles that are unavailable. System.Drawing.Font only supports TrueType fonts.Sternutation
possible duplicate of Retrieving available font sizes on WindowsVacant
P
1

You could use the InstalledFontCollection class to retrieve the available fonts and then enumerate them as shown in this MSDN article.

InstalledFontCollection installedFontCollection = new InstalledFontCollection();

// Get the array of FontFamily objects.
fontFamilies = installedFontCollection.Families;

// The loop below creates a large string that is a comma-separated
// list of all font family names.

int count = fontFamilies.Length;
for (int j = 0; j < count; ++j)
{
    familyName = fontFamilies[j].Name;
    familyList = familyList + familyName;
    familyList = familyList + ",  ";
}
Preemie answered 28/12, 2011 at 20:57 Comment(3)
I already have working font name combo - but how can I get the sizes and styles the font supports?Junction
FontFamily has a IsStyleAvailable method: msdn.microsoft.com/en-us/library/… which returns a bool. Not sure about font size.Preemie
If it is an outline font (TrueType, etc.) then any size should be possible. I am not sure how that applies to bitmap fonts.Functionalism
B
1

I was trying to find a good-looking font family today, I use the below code to enumerate all font families, and print them in an image, so that it is easier to compare which looks good.

Shared below:

        Bitmap bitmapImage = new Bitmap(width: 1600, height: 8000);
        using (Graphics g = Graphics.FromImage(bitmapImage))
        {
            var imageRect = new Rectangle(x: 0, y: 0, width: 1600, height: 8000);

            System.Drawing.Text.InstalledFontCollection installedFontCollection = new System.Drawing.Text.InstalledFontCollection();
            FontFamily[] fontFamilies = installedFontCollection.Families;

            var format = new StringFormat();
            format.Alignment = StringAlignment.Near;
            format.LineAlignment = StringAlignment.Near;
            format.FormatFlags = StringFormatFlags.NoWrap;

            int verticalOffset = 0;
            for (int j = 0; j < fontFamilies.Length; ++j)
            {
                using (var font = new Font(fontFamilies[j].Name, 40, FontStyle.Regular, GraphicsUnit.Pixel))
                {
                    // Height
                    var textSize = g.MeasureString(fontFamilies[j].Name, font);
                    int textWidth = (int)Math.Ceiling(textSize.Width + 10);
                    int textHeight = (int)Math.Ceiling(textSize.Height + 10);

                    // Draw text
                    Rectangle textRect = new Rectangle(x: j % 2 == 0 ? 0 : 800, y: verticalOffset, width: textWidth, height: textHeight);
                    g.FillRectangle(new SolidBrush(BackgroundColor), textRect);
                    g.DrawString(fontFamilies[j].Name, font, new SolidBrush(PercentageTextColor), textRect, format);
                    g.Save();

                    if (j % 2 == 1)
                    {
                        verticalOffset += textHeight;
                    }
                }
            }
        }

        bitmapImage.Save(this.Response.OutputStream, ImageFormat.Png);


        // then do whatever you like with this bitmapImage, save it to local, etc.
Berk answered 1/12, 2016 at 3:7 Comment(0)
C
0

I know I am late to the party, but for anyone else who may stumble across this I will leave what I ended up doing.

  1. For available font families & styles I used the selected answer and the comments for it.

  2. To get all sizes for a font, I found that the answer provided by the user 'Thread1' has been reliable.


Edit - Added the essential information rather than just a link:

I believe font families are not limited to specific range of points only, they can be set with any values. They are hardcoded. However, when the size in points is converted to pixel it must result to an integer value. Thus when you specify "8 pts." it will be converted to "8.25 pts.". You can use the following function to get the actual point size... Code:

private float GetActualPoints(float points)
    return (float) Math.Round(points / 0.75f) * 0.75f;
}

The "0.75" constant is the points/pixel (ratio) Busy

Cavalry answered 28/6, 2024 at 14:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.