小编Ada*_*ohl的帖子

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

我在资源中嵌入了一个.ttf字体文件(特别是“ Amatic Bold”),并在下面使用此代码获取字体。我在这篇文章中尝试了以下代码:如何在C#应用程序中嵌入字体?(使用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);
    }
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

Font font = GetCustomFont(Properties.MainResources.Amatic_Bold, 25, System.Drawing.FontStyle.Bold);     
Run Code Online (Sandbox Code Playgroud)

字体应如下所示: 在此处输入图片说明

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

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

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

c# fonts embedded-resource winforms

5
推荐指数
1
解决办法
2507
查看次数

标签 统计

c# ×1

embedded-resource ×1

fonts ×1

winforms ×1