微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

testing是否安装了字体

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

在收到所有POST数据之前,PHP脚本是否可以启动?

为什么我的WPF应用程序@R_404_6301@?

在.NET控制台应用程序中,防止Windows睡眠/hibernate

HKLM SYstem CurrentControlSet Control TimeZone@R_317_4045@ion TimeZoneKeyName损坏?

.Net TimeZoneInfo ID – Windows语言特定?

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 fontFamiliy in fontsCollection.Families) { if (fontFamiliy.Name == fontName) ... \ installed }

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

MSDN:

枚举安装的字体

感谢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); } }

建议使用Font创建的其他答案只有在FontStyle.Regular可用时才有效。 一些字体,例如Verlag Bold,没有正规风格。 创建将失败,例外Font'Verlag Bold'不支持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,StringComparison.InvariantCultureIgnoreCase)); } } catch { } return installed; }

走出GvS的答案:

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

以下是我将如何做到这一点:

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的字体预览工具中的字体的名称。)

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐