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?
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?
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 + ", ";
}
FontFamily
has a IsStyleAvailable
method: msdn.microsoft.com/en-us/library/… which returns a bool. Not sure about font size. –
Preemie 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.
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.
For available font families & styles I used the selected answer and the comments for it.
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
© 2022 - 2025 — McMap. All rights reserved.