在VB.net/C#中直接使用资源字体

发布于 2024-09-03 04:04:35 字数 59 浏览 1 评论 0原文

如何在VB.net/C#中为独立应用程序[桌面应用程序]直接使用资源字体而不将字体保存在本地文件系统中?

How to use resource font directly without saving font in local file system for standalone application[desktop application] in VB.net/C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葵雨 2024-09-10 04:04:35

这是可能的,您需要使用 PrivateFontCollection.AddMemoryFont() 方法。例如,我添加了一个名为“test.ttf”的字体文件作为资源,并按如下方式使用它:

using System.Drawing.Text;
using System.Runtime.InteropServices;
...
public partial class Form1 : Form {
    private static PrivateFontCollection myFonts;
    private static IntPtr fontBuffer;

    public Form1() {
        InitializeComponent();
        if (myFonts == null) {
            myFonts = new PrivateFontCollection();
            byte[] font = Properties.Resources.test;
            fontBuffer = Marshal.AllocCoTaskMem(font.Length);
            Marshal.Copy(font, 0, fontBuffer, font.Length);
            myFonts.AddMemoryFont(fontBuffer, font.Length);
        }
    }

    protected override void OnPaint(PaintEventArgs e) {
        FontFamily fam = myFonts.Families[0];
        using (Font fnt = new Font(fam, 16)) {
            TextRenderer.DrawText(e.Graphics, "Private font", fnt, Point.Empty, Color.Black);
            //e.Graphics.DrawString("Private font", fnt, Brushes.Black, 0, 0);
        }
    }
}

请注意,fontBuffer 变量是有意静态的。当您使用 AddMemoryFont() 时,内存管理很困难,只要可以使用字体并且 PrivateFontCollection 尚未释放,内存就需要保持有效。如果没有这样的保证,请确保不要调用 Marshal.FreeCoTaskMem(),这是一个非常常见的错误,会导致很难诊断文本损坏。仅当您幸运时才会遇到 AccessViolationException。保持它在程序的生命周期内有效是简单的解决方案。

That's possible, you'll need to use the PrivateFontCollection.AddMemoryFont() method. For example, I added a font file named "test.ttf" as a resource and used it like this:

using System.Drawing.Text;
using System.Runtime.InteropServices;
...
public partial class Form1 : Form {
    private static PrivateFontCollection myFonts;
    private static IntPtr fontBuffer;

    public Form1() {
        InitializeComponent();
        if (myFonts == null) {
            myFonts = new PrivateFontCollection();
            byte[] font = Properties.Resources.test;
            fontBuffer = Marshal.AllocCoTaskMem(font.Length);
            Marshal.Copy(font, 0, fontBuffer, font.Length);
            myFonts.AddMemoryFont(fontBuffer, font.Length);
        }
    }

    protected override void OnPaint(PaintEventArgs e) {
        FontFamily fam = myFonts.Families[0];
        using (Font fnt = new Font(fam, 16)) {
            TextRenderer.DrawText(e.Graphics, "Private font", fnt, Point.Empty, Color.Black);
            //e.Graphics.DrawString("Private font", fnt, Brushes.Black, 0, 0);
        }
    }
}

Do note that the fontBuffer variable is static intentionally. Memory management is difficult when you use AddMemoryFont(), the memory needs to remain valid as long as the font can be used and the PrivateFontCollection is not yet disposed. Be sure not to call Marshal.FreeCoTaskMem() if you don't have that guarantee, it is a very common bug that causes very hard to diagnose text corruption. You only get an AccessViolationException when you are lucky. Keeping it valid for the life of the program is the simple solution.

眉黛浅 2024-09-10 04:04:35

您是在谈论用应用程序打包字体吗?如果是。
看看这个:
http://msdn.microsoft.com/en-us/library/ms753303。 ASPX

Are you talking about Packaging fonts with application. if Yes.
check out this:
http://msdn.microsoft.com/en-us/library/ms753303.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文