.net - PrivateFontCollection 与 gdi+ 有时在 WIndows 8 或更高版本中使用错误的 FontStyle

标签 .net fonts gdi+ system.drawing privatefontcollection

如果您有从 PrivateFontCollection 创建的字体,并尝试使用 GDI+ 用它绘制字符串,有时会使用错误的 FontStyle。

我在从内存加载到 PrivateFontCollection 的字体和从文件加载到 PrivateFontCollection 的字体时都观察到了这一点。

在下面的示例中,我从文件加载字体。我把它们全部放在一个名为“字体”的文件夹中。如果我们像下面这样加载它们。

    private void loadFontsIntoPrivateCollection()
    {
        _privateFontCollection = new PrivateFontCollection();
        _families = new Dictionary<string, FontFamily>();

        var files = Directory.GetFiles("Fonts");
        foreach (var file in files)
            _privateFontCollection.AddFontFile(file);

        foreach (var fontFamily in _privateFontCollection.Families)
        {
            _families.Add(fontFamily.Name, fontFamily);
            comboBox1.Items.Add(fontFamily.Name);
        }

        comboBox1.Text = "Californian FB";
    }

然后尝试使用以下代码在 OnPaint 上绘制字符串

    private void panelTextArea_Paint(object sender, PaintEventArgs e)
    {
        var familyToUse = _families[comboBox1.Text];
        var fontToUse = new Font(familyToUse, 28, _styleToUse, GraphicsUnit.World);
        e.Graphics.DrawString(textBox1.Text, fontToUse, Brushes.Black, panelTextArea.ClientRectangle);
    }

那么在大多数情况下,字符串将被绘制为与 FontStyle.Regular 对应的字体。我注意到,如果您先小心加载常规字体,那么 FontFamily 会伪造除 FontStyle.Regular 之外的任何 FontStyle。无论您是否已将它们加载到 PrivateFontCollection 中。

以下是我在其中看到过这种行为的一些字体。

  • 于哥特式
  • 加州 Facebook
  • Tw Cen MT
  • Gill Sans MT
  • Berlin Sans FB
  • BrowalliaUPC
  • 传统阿拉伯语

在graphicsPath 上执行Graphics.DrawString() 和AddString 时,我看到了这种行为。使用 TextRenderer.DrawText() 将正确绘制字体。

您应该能够在 Windows 8 或 10 中轻松地重新创建此行为,只需使用 PrivateFontCollection https://msdn.microsoft.com/en-us/library/windows/desktop/ms533820(v=vs.85).aspx 的示例代码即可。

根据我与 Microsoft 就该行为的通信,他们认为这是 gdi+ 的问题。

最佳答案

我们注意到,如果您只将一种 FontStyle 加载到 PrivateFontCollection 中,它将始终使用该 FontStyle。

我们所做的是包装 PrivateFontCollection 和 FontFamily 类,因此当我们将文件中的字体加载到 PrivateFontCollection 中时,它实际上会将其添加到 4 个 PrivateFontCollection 之一,具体取决于字体文件具有的 FontStyle。

然后,我们通过调用自己的 FontFamily 类来创建字体,该类具有从适当的 PrivateFontCollection 中选择字体的逻辑。

关于.net - PrivateFontCollection 与 gdi+ 有时在 WIndows 8 或更高版本中使用错误的 FontStyle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31140819/

相关文章:

c# - 如何优化此代码以创建 TIFF 文件和/或我应该考虑使用哪些替代库来提高性能?

.net - FillRectangle 和 DrawRectangle 的像素行为

.net - 为什么 .NET 字典发牢骚?

c# - 使用反射确定事件是否是静态的

css - Sigil 中的字体系列更改

linux - OpenVG UTF 8/unicode 字符

c# - 如何将图像裁剪成圆形?

.net - 将数据从 ODBC 源合并到 SQL Server 数据库的最有效方法

.NET - 是否可以声明只能访问命名空间的模块成员?

css - 我如何在 CSS 中使用自定义字体