2008-09-22 28 views

答えて

23
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 
    } 
} 
+6

これは、通常のフォントスタイルが利用可能な場合にのみ有効です – jltrem

15

How do you get a list of all the installed fonts?

var fontsCollection = new InstalledFontCollection(); 
foreach (var fontFamiliy in fontsCollection.Families) 
{ 
    if (fontFamiliy.Name == fontName) ... \\ installed 
} 

詳細についてはInstalledFontCollection classを参照してください。

MSDN:
Enumerating Installed Fonts

+3

これは私にとって最も直接的な方法ですが、他の提案された回答は回避策のように見えます。 –

+0

@UgurTuran:はい、パフォーマンスについては不思議ですね。あなたが試してみるまで知りませんでしたが、他の人はテーブルルックアップの機会があってより速いと言っています。 – Hans

12

ジェフのおかげで、私はより良いFontクラスのドキュメントを読んでいる:

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

この知識の結果:GVS」と答えたのオフに行く

private bool IsFontInstalled(string fontName) { 
     using (var testFont = new Font(fontName, 8)) { 
      return 0 == string.Compare(
       fontName, 
       testFont.Name, 
       StringComparison.InvariantCultureIgnoreCase); 
     } 
    } 
+3

このFontコンストラクタは、FontStyle.Regularを想定しています。通常のフォントスタイルが使用可能な場合にのみ機能します – jltrem

2

private static bool IsFontInstalled(string fontName) 
    { 
     using (var testFont = new Font(fontName, 8)) 
      return fontName.Equals(testFont.Name, StringComparison.InvariantCultureIgnoreCase); 
    } 
5

他の回答はFontStyle.Regularが利用可能な場合にのみ動作しFont作成を使用して提案しました。 Verlag Boldなどのフォントには、通常のスタイルはありません。 フォント 'Verlag Bold'はスタイル '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; 
    } 
1

は、ここで私はそれを行うだろう方法は次のとおりです。これに注意する

private static bool IsFontInstalled(string name) 
{ 
    using (InstalledFontCollection fontsCollection = new InstalledFontCollection()) 
    { 
     return fontsCollection.Families 
      .Any(x => x.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)); 
    } 
} 

一つはNameプロパティは、Cで見ているから期待するものとは限らないということである:\ WINDOWS \ Fonts。たとえば、 "Arabic Typsetting Regular"というフォントがインストールされています。 IsFontInstalled("Arabic Typesetting Regular")はfalseを返しますが、IsFontInstalled("Arabic Typesetting")はtrueを返します。 (「アラビア語タイプセット」は、Windowsのフォントプレビューツールのフォント名です。)

リソースに関しては、このメソッドを何度か呼び出してテストを実行しましたが、数ミリ秒でテストが完了しました毎回。私のマシンは圧倒されていますが、このクエリを頻繁に実行する必要がない限り、パフォーマンスは非常に良いと思われます(キャッシングを行ったとしても、それがキャッシュになります)。