windows 测试是否安装了字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/113989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:16:32  来源:igfitidea点击:

Test if a Font is installed

.netwindowsfonts

提问by GvS

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

有没有一种简单的方法(在 .Net 中)来测试当前机器上是否安装了字体?

采纳答案by Jeff Hillman

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
    }
}

回答by aku

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

如何获得所有已安装字体的列表?

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

See InstalledFontCollection classfor details.

有关详细信息,请参阅InstalledFontCollection 类

MSDN:
Enumerating Installed Fonts

MSDN:
枚举已安装的字体

回答by GvS

Thanks to Jeff, I have better read the documentation of the Font class:

感谢 Jeff,我最好阅读 Font 类的文档:

If the familyName parameter specifies a font that is not installed on the machine running the application or is not supported, Microsoft Sans Serif will be substituted.

如果 familyName 参数指定的字体未安装在运行应用程序的计算机上或不受支持,则 Microsoft Sans Serif 将被替换。

The result of this knowledge:

这个知识的结果:

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

回答by jltrem

Other answers proposed using Fontcreation only work if the FontStyle.Regularis 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:

使用Font创建提出的其他答案仅在FontStyle.Regular可用时才有效。某些字体,例如 Verlag Bold,没有常规样式。创建将失败,异常Font '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;
  }

回答by nateirvin

Here's how I would do it:

这是我将如何做到的:

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

One thing to note with this is that the Nameproperty is not always what you would expect from looking in C:\WINDOWS\Fonts. For example, I have a font installed called "Arabic Typsetting Regular". IsFontInstalled("Arabic Typesetting Regular")will return false, but IsFontInstalled("Arabic Typesetting")will return true. ("Arabic Typesetting" is the name of the font in Windows' font preview tool.)

需要注意的一件事是,该Name属性并不总是您在 C:\WINDOWS\Fonts 中所期望的。例如,我安装了一种名为“Arabic Typsetting Regular”的字体。IsFontInstalled("Arabic Typesetting Regular")会返回假,但IsFontInstalled("Arabic Typesetting")会返回真。(“Arabic Typesetting”是Windows 字体预览工具中的字体名称。)

As far as resources go, I ran a test where I called this method several times, and the test finished in only a few milliseconds every time. My machine's a bit overpowered, but unless you'd need to run this query very frequently it seems the performance is very good (and even if you did, that's what caching is for).

就资源而言,我运行了一个测试,多次调用此方法,每次测试仅在几毫秒内完成。我的机器有点强大,但除非你需要非常频繁地运行这个查询,否则它的性能似乎非常好(即使你这样做了,这就是缓存的用途)。

回答by Hans

Going off of GvS' answer:

离开 GvS 的回答:

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

回答by Sourcephy

In my case I need to check font filename with extension

在我的情况下,我需要检查带有扩展名的字体文件名

ex: verdana.ttf = Verdana Regular, verdanai.ttf = Verdana Italic

例如:verdana.ttf = Verdana 常规,verdanai.ttf = Verdana 斜体

using System.IO;

IsFontInstalled("verdana.ttf")

public bool IsFontInstalled(string ContentFontName)
{
    return File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), ContentFontName.ToUpper()));
}