测试是否安装了字体

发布于 2024-07-06 00:47:40 字数 42 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

jJeQQOZ5 2024-07-13 00:47:40

离开 GvS 的答案:

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

Going off of GvS' answer:

    private static bool IsFontInstalled(string fontName)
    {
        using (var testFont = new Font(fontName, 8))
            return fontName.Equals(testFont.Name, StringComparison.InvariantCultureIgnoreCase);
    }
远昼 2024-07-13 00:47:40

就我而言,我需要检查扩展名

ex 的字体文件名:verdana.ttf = Verdana Regular,verdanai.ttf = Verdana Italic

using System.IO;

IsFontInstalled("verdana.ttf")

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

In my case I need to check font filename with extension

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

using System.IO;

IsFontInstalled("verdana.ttf")

public bool IsFontInstalled(string ContentFontName)
{
    return File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), ContentFontName.ToUpper()));
}
温柔女人霸气范 2024-07-13 00:47:40

我将这样做:

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。 (“Arabic Typesetting”是 Windows 字体预览工具中字体的名称。)

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

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 Name property 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.)

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).

千笙结 2024-07-13 00:47:40

使用 Font 创建提出的其他答案仅在 FontStyle.Regular 可用时才有效。 某些字体(例如 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;
  }

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;
  }
李白 2024-07-13 00:47:40
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
    }
}
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
    }
}

如何获取所有内容的列表安装的字体?

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

请参阅InstalledFontCollection 类 详细信息。

MSDN:
枚举已安装的字体

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 class for details.

MSDN:
Enumerating Installed Fonts

孤单情人 2024-07-13 00:47:40

感谢 Jeff,我更好地阅读了 Font 类的文档:

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

这些知识的结果:

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

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

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.

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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文