c# - 为什么 System.Drawing.Fontconverter.ConvertFromInvariantString ("NOTAFONT")返回 SansSerif?

标签 c# serialization fonts

为什么

var fontConverter = new FontConverter();            
var retFont = fontConverter.ConvertFromInvariantString("NOTAFONT");

return {Name = "Microsoft Sans Serif" Size=8.25}

并且不抛出异常?

使用此代码,我打算测试一个字符串是否包含有效的字体序列化。还有其他方法可以检查吗?

我检查了 FontConverter 类的代码,它创建了如下所示的 returnfont:

 var retFont = new Font("notafont", 1.2f, FontStyle.Bold);

返回的字体为{Name = "Microsoft Sans Serif"Size=1.2}

最佳答案

查看 MSDN Documentation on the Font Class备注部分有一个有趣的说法。

即:

For more information about how to construct fonts, see How to: Construct Font Families and Fonts. Windows Forms applications support TrueType fonts and have limited support for OpenType fonts. If you attempt to use a font that is not supported, or the font is not installed on the machine that is running the application, the Microsoft Sans Serif font will be substituted.

我认为您正在看到这种行为。

不确定这是否能满足您的需求,但在本例中我使用 InstalledFontCollection验证字体是否可用。

bool CheckFontAvailability(string value)
{
    FontCollection fc = new InstalledFontCollection();
    return fc.Families.Any(ff => ff.Name.Contains(value));
}

或由 schoetbi 修改

 bool CheckFontAvailability(string fontAsString)
{
    FontCollection fc = new InstalledFontCollection(); 
    return fc.Families.Any(ff => fontAsString.StartsWith(ff.Name));
}

关于c# - 为什么 System.Drawing.Fontconverter.ConvertFromInvariantString ("NOTAFONT")返回 SansSerif?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24750045/

相关文章:

json - 在 Serde 中处理混合对象数组

java - 通过序列化恢复单个实例

java - 如何忽略 PDFBox 2.0.7 使用的字体中缺失的字形

c# - 如何在 C# 中混合文本框中的单词顺序?

C# 扩展 (OmniSharp) 在 VSCode 中给出错误结果

c# - 动态设置 DllImport 属性

caching - 如何配置 Spring Redis 配置以使用 Hash 而不是字符串序列化

iOS 不完整的字体(复选标记)?

html - 如何正确显示与字体粗细相关的 'style'?

c# - 用于开发的本地事件目录访问和管理?