c# - C# 中的嵌入式资源字体无法正常工作

标签 c# winforms fonts embedded-resource

我在我的资源中嵌入了一个 .ttf 字体文件(特别是“Amatic Bold”),我正在使用下面的代码获取字体。 我尝试了这篇文章中的代码:How do I Embed a font with my C# application? (using Visual Studio 2005)

这是我的实现:

    static public Font GetCustomFont (byte[] fontData, float size, FontStyle style)
    {
        if (_fontCollection == null) _fontCollection = new PrivateFontCollection();
        IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
        System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
        _fontCollection.AddMemoryFont(fontPtr, fontData.Length);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
        return new Font(_fontCollection.Families[0], size, style);
    }

我是这样使用它的:

Font font = GetCustomFont(Properties.MainResources.Amatic_Bold, 25, System.Drawing.FontStyle.Bold);     

字体应该是这样的: enter image description here

问题是字体正在加载,但在使用时显示不正确;它看起来像“Arial”或其他标准字体,而不是它应有的样子。 如果我在 Windows 中安装该字体,它就可以工作(我想这很明显......)

我搜索了现有的答案,但找不到我的确切问题...

任何帮助将不胜感激。 提前致谢。

最佳答案

好吧,那么...我想我明白了!

我将解释我“发现”的内容(无论是否显而易见):

  • 首先:Application.SetCompatibleTextRenderingDefault 必须设置为 true 才能在控件中呈现内存字体。 (也可以使用 Control.UseCompatibleTextRendering) 它在 Microsoft 文档中有完美的说明,但我错过了:-(

  • 第二:PrivateFontCollection.Families 返回一组添加的字体,但是.. 惊喜!它是按字母顺序排列的! 无论您添加字体的顺序是什么或使用的方法是什么 (AddMemoryFont/AddFontFile),您都将按字母顺序获得它! 因此,如果您要添加不止一种字体,然后尝试获取您添加的最后一种字体,您可能会得到错误的字体。

  • 第三:我也尝试过在集合中添加字体后执行 FreeCoTaskMem() 或在表单关闭时执行此操作。两者都在为我工作! 我不知道这个的确切含义...

这是我的最终代码:

    //This list is used to properly dispose PrivateFontCollection after usage
    static private List<PrivateFontCollection> _fontCollections;

    [STAThread]
    private static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);    //Mandatory in order to have Memory fonts rendered in the controls.

        //Dispose all used PrivateFontCollections when exiting
        Application.ApplicationExit += delegate {
            if (_fontCollections != null) {
                foreach (var fc in _fontCollections) if (fc != null) fc.Dispose();
                _fontCollections = null;
            }
        };

        Application.Run(new frmMain());
    }

    void frmMain_Load(object sender, EventArgs e)
    {
        Font font1 = GetCustomFont(Properties.Resources.Amatic_Bold, 25, FontStyle.Bold);
        //or...
        Font font1 = GetCustomFont("Amatic-Bold.ttf", 25, FontStyle.Bold);

        labelTestFont1.Font = font1;

        Font font2 = GetCustomFont(Properties.Resources.<font_resource>, 25, FontStyle.Bold);
        //or...
        Font font2 = GetCustomFont("<font_filename>", 25, FontStyle.Bold);

        labelTestFont2.Font = font2;

        //...

    }
    static public Font GetCustomFont (byte[] fontData, float size, FontStyle style)
    {
        if (_fontCollections == null) _fontCollections = new List<PrivateFontCollection>();
        PrivateFontCollection fontCol = new PrivateFontCollection();
        IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length);
        Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
        fontCol.AddMemoryFont(fontPtr, fontData.Length);
        Marshal.FreeCoTaskMem(fontPtr);     //<-- It works!
        _fontCollections.Add (fontCol);
        return new Font(fontCol.Families[0], size, style);
    }


    static public Font GetCustomFont (string fontFile, float size, FontStyle style)
    {
        if (_fontCollections == null) _fontCollections = new List<PrivateFontCollection>();
        PrivateFontCollection fontCol = new PrivateFontCollection();
        fontCol.AddFontFile (fontFile);
        _fontCollections.Add (fontCol);
        return new Font(fontCol.Families[0], size, style);
    }

如您所见,我决定为每种字体创建一个独占的 PrivateFontCollection,然后将其存储到一个列表中,以便在应用程序端进行最终处理。

这已在 3 台不同的 PC(均使用 Windows 7、32 和 64 位)和 3 种不同的 .ttf 字体中进行了测试。

结果示例:

enter image description here

我不知道我的方法是否足够好,但我希望它对其他人有用!

更多细节: 与我的预期不同,AddMemoryFont 比 AddFontFile 慢(21 毫秒对 15 毫秒)

再次感谢大家的评论!

关于c# - C# 中的嵌入式资源字体无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36486014/

相关文章:

c# - 使用安装在 IIS 上的 URL Rewrite 配置 Web API

c# - 在事件处理 C# 中创建事件变量需要什么?

c# - form2 关闭并重新显示 form1 后 Form1 不会保持打开状态

c# - 如何在 C#.net 中的 Combobox 中添加值

delphi - 我可以使用 Delphi 将用户限制为 Windows 应用程序中的字体子集吗?

android - 当我们更改三星设备中的设备字体样式时如何防止 TextView 字体更改

c# - 如何使用 float.Parse 从 "5/2"等字符串中获取小数

c# - XAML 是否适用于 Visual Studio 中的文件链接?

c# - 如何在 winforms 中为整个应用程序设置通用时区

html - 谷歌字体无法使用