我在资源中嵌入了一个.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中,则可以正常工作(我想很明显...)
我搜索了现有的答案,但找不到确切的问题...
任何帮助将不胜感激。提前致谢。