Test if a Font is installed

前端 未结 7 1387
日久生厌
日久生厌 2020-11-30 08:34

Is there an easy way (in .Net) to test if a Font is installed on the current machine?

相关标签:
7条回答
  • 2020-11-30 09:24

    Other answers proposed using Font creation only work if the FontStyle.Regular is available. Some fonts, for example Verlag Bold, do not have a regular style. Creation would fail with exception Font 'Verlag Bold' does not support style 'Regular'. You'll need to check for styles that your application will require. A solution follows:

      public static bool IsFontInstalled(string fontName)
      {
         bool installed = IsFontInstalled(fontName, FontStyle.Regular);
         if (!installed) { installed = IsFontInstalled(fontName, FontStyle.Bold); }
         if (!installed) { installed = IsFontInstalled(fontName, FontStyle.Italic); }
    
         return installed;
      }
    
      public static bool IsFontInstalled(string fontName, FontStyle style)
      {
         bool installed = false;
         const float emSize = 8.0f;
    
         try
         {
            using (var testFont = new Font(fontName, emSize, style))
            {
               installed = (0 == string.Compare(fontName, testFont.Name, StringComparison.InvariantCultureIgnoreCase));
            }
         }
         catch
         {
         }
    
         return installed;
      }
    
    0 讨论(0)
提交回复
热议问题