31

フォントが現在のマシンにインストールされているかどうかを (.Net で) テストする簡単な方法はありますか?

4

7 に答える 7

31
string fontName = "Consolas";
float fontSize = 12;

using (Font fontTester = new Font( 
       fontName, 
       fontSize, 
       FontStyle.Regular, 
       GraphicsUnit.Pixel)) 
{
    if (fontTester.Name == fontName)
    {
        // Font exists
    }
    else
    {
        // Font doesn't exist
    }
}
于 2008-09-22T09:39:17.213 に答える
24

インストールされているすべてのフォントのリストを取得するにはどうすればよいですか?

var fontsCollection = new InstalledFontCollection();
foreach (var fontFamily in fontsCollection.Families)
{
    if (fontFamily.Name == fontName) {...} \\ check if font is installed
}

詳細については、 InstalledFontCollection クラスを参照してください。

MSDN:
インストールされているフォントの列挙

于 2008-09-22T09:38:57.590 に答える
15

Jeff のおかげで、Font クラスのドキュメントをよく読むことができました。

アプリケーションを実行しているマシンにインストールされていない、またはサポートされていないフォントが familyName パラメーターで指定されている場合は、Microsoft Sans Serif が代わりに使用されます。

この知識の結果:

    private bool IsFontInstalled(string fontName) {
        using (var testFont = new Font(fontName, 8)) {
            return 0 == string.Compare(
              fontName,
              testFont.Name,
              StringComparison.InvariantCultureIgnoreCase);
        }
    }
于 2008-09-22T09:56:24.397 に答える
6

作成を使用して提案された他の回答は、利用可能なFont場合にのみ機能しFontStyle.Regularます。Verlag Bold などの一部のフォントには、通常のスタイルがありません。作成は例外Font 'Verlag Bold' does not support style 'Regular'で失敗します。アプリケーションに必要なスタイルを確認する必要があります。解決策は次のとおりです。

  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;
  }
于 2013-07-11T14:57:29.977 に答える
2

GvSの答えから外れる:

    private static bool IsFontInstalled(string fontName)
    {
        using (var testFont = new Font(fontName, 8))
            return fontName.Equals(testFont.Name, StringComparison.InvariantCultureIgnoreCase);
    }
于 2013-02-01T19:35:17.853 に答える