2261

.NET System.Drawing.Font - Get Available Sizes and Styles

Question:

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?

Answer1:

You could use the <a href="http://msdn.microsoft.com/en-us/library/system.drawing.text.installedfontcollection_members%28v=VS.90%29.aspx" rel="nofollow">InstalledFontCollection</a> class to retrieve the available fonts and then enumerate them as shown <a href="http://msdn.microsoft.com/en-us/library/0yf5t4e8%28v=VS.90%29.aspx" rel="nofollow">in this MSDN article</a>.

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 + ", "; }

Answer2:

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.

Recommend

  • Maximize Database View With Multiple Or Statement
  • Re-implementing lists with closures in scheme
  • apply fitted model to data and obtain loglikelihood
  • How to import fonts with pure JS? [duplicate]
  • Extract text from “” HTML tag
  • Docker container for google cloudML on compute engine - authenticating for mounting bucket
  • UIImageJPEGRepresentation giving 2x images on retina display
  • IF statement formatting best-practise, what's your style?
  • Plot a CSV file where the delimiter is '; ' (semicolon + space)
  • branch out of range compile error
  • Why are views not counted if you embed a youtube iframe dynamically using jquery?
  • Cassandra: What is a subcolumn
  • C++ Armadillo Access Triangular Matrix Elements
  • How to use arithmetic operators with SAS macro variables [duplicate]
  • How to remove comma or any characters from Python dataframe column name
  • Binary Tree Traversal Sum Of Each Depth
  • Small video playback
  • Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY)
  • Creating JS objects in PHP with commas in between
  • Selecting a subset of data in ServiceStack.OrmLite
  • Is there any purpose for h2-h6 headings in HTML5?
  • Prolog Ambiguous Output
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • Checking if an array in C is symmetric
  • Installed module is empty
  • CSS Linear-gradient formatting issue accross different browsers
  • What is Eclipse's Declaration View used for?
  • AT Commands to Send SMS not working in Windows 8.1
  • Traverse Array and Display in markup
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How do I configure my settings file to work with unit tests?
  • Turn off referential integrity in Derby? is it possible?
  • apache spark aggregate function using min value
  • Add sale price programmatically to product variations
  • Sorting a 2D array using the second column C++
  • Unable to use reactive element in my shiny app
  • Python/Django TangoWithDjango Models and Databases
  • How do I use LINQ to get all the Items that have a particular SubItem?