2011-12-28 52 views
0

我有一個組合,允許用戶選擇一個字體名稱。.NET System.Drawing.Font - 獲取可用的大小和樣式

第二個應該顯示可用的字體大小。第三個必須展示可用的樣式。

問題:如何檢索System.Drawing.Font支持的尺寸和樣式?

+1

你將不得不回到Windows版本3地發現,僅在一定的尺寸可供選擇的字體。設備字體。 TrueType字體可以呈現任何大小。並支持不可用的綜合樣式。 System.Drawing.Font只支持TrueType字體。 – 2011-12-28 21:25:53

+0

可能重複的[在Windows上檢索可用字體大小](http://stackoverflow.com/questions/1003450/retrieving-available-font-sizes-on-windows) – LarsTech 2011-12-28 21:28:30

回答

1

您可以使用InstalledFontCollection類來檢索可用字體,然後如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 + ", "; 
} 
+1

我已經有工作字體名稱組合 - 但我怎麼能獲取字體支持的尺寸和樣式? – SharpAffair 2011-12-28 21:00:15

+0

'FontFamily'有一個'IsStyleAvailable'方法:http://msdn.microsoft.com/en-us/library/system.drawing.fontfamily.isstyleavailable%28v=VS.90%29.aspx它返回一個布爾值。不確定字體大小。 – keyboardP 2011-12-28 21:02:47

+0

如果它是一個輪廓字體(TrueType等),那麼任何大小都應該是可能的。我不知道如何適用於位圖字體。 – user957902 2011-12-28 21:34:57

0

我今天要找到一個好看的字體,我用下面的代碼枚舉所有字體系列,並打印出來的圖像,使其更容易比較這看起來很不錯。

以下共享:

 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.