测试是否安装了字体

时间:2020-03-06 14:31:53  来源:igfitidea点击:

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

解决方案

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

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

有关详细信息,请参见InstalledFontCollection类。

MSDN:
枚举安装的字体

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

感谢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.

该知识的结果:

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