简体   繁体   English

使用自定义字体问题

[英]Use custom font issue

I have a custom font file that i want to use on my application , currently i am using .net framework2 and here is the code i used :我有一个要在我的应用程序中使用的自定义字体文件,目前我使用的是 .net framework2,这是我使用的代码:

private void Form1_Load(object sender, EventArgs e)
{
    //load the resource
    Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("Eurostile_Regular_0.ttf");

    //create an unsafe memory block for the data
    System.IntPtr data = Marshal.AllocCoTaskMem(Convert.ToInt16(fontStream.Length));

    //create a buffer to read in to
    byte[] fontdata = null;
    fontdata = new byte[fontStream.Length + 1];

    //fetch the font program from the resource
    fontStream.Read(fontdata, 0, Convert.ToInt16(fontStream.Length));

    //copy the bytes to the unsafe memory block
    Marshal.Copy(fontdata, 0, data, Convert.ToInt16(fontStream.Length));

    //pass the font to the font collection
    pfc.AddMemoryFont(data, Convert.ToInt16(fontStream.Length));

    //close the resource stream
    fontStream.Close();

    //free the unsafe memory
    Marshal.FreeCoTaskMem(data);
}

And on Form1_Paint :在 Form1_Paint 上:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    bool bold = false;
    bool regular = false;
    bool italic = false;

    e.Graphics.PageUnit = GraphicsUnit.Point;

    SolidBrush b = new SolidBrush(Color.Black);

    float y = 5;

    System.Drawing.Font fn;

    foreach (FontFamily ff in pfc.Families)
    {
        if (ff.IsStyleAvailable(FontStyle.Regular))
        {
            regular = true;
            fn = new Font(ff, 18, FontStyle.Regular);
            e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
            fn.Dispose();
            y += 20;
        }

        if (ff.IsStyleAvailable(FontStyle.Bold))
        {
            bold = true;
            fn = new Font(ff, 18, FontStyle.Bold);
            e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
            fn.Dispose();
            y += 20;
        }

        if (ff.IsStyleAvailable(FontStyle.Italic))
        {
            italic = true;
            fn = new Font(ff, 18, FontStyle.Italic);
            e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
            fn.Dispose();
            y += 20;
        }

        if (bold && italic)
        {
            fn = new Font(ff, 18, FontStyle.Bold | FontStyle.Italic);
            e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
            fn.Dispose();
            y += 20;
        }
        fn = new Font(ff, 18, FontStyle.Underline);
        e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
        fn.Dispose();
        y += 20;
        fn = new Font(ff, 18, FontStyle.Strikeout);
        e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
        fn.Dispose();
    }
    b.Dispose();
}

But the font is not working and i get a JIT error from the second line on form_load但是字体不起作用,我从 form_load 的第二行收到 JIT 错误

System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:未将对象引用设置为对象的实例。

First go to your Font Properties -> Build Action -> "Embedded Resource"首先转到您的Font Properties -> Build Action -> "Embedded Resource"

Second your problem is that you forgot to add your NameSpace name in the code其次,您的问题是您忘记在代码中添加您的 NameSpace 名称

Wrong format格式错误

Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("FileName.ttf");

Correct format正确的格式

Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("NameSpaceName.FileName.ttf");

Good luck祝你好运

The fontStream is null because GetManifestResourceStream could not find your font. fontStream 为空,因为 GetManifestResourceStream 找不到您的字体。 Your font has to be added to the solution and then choosing "embedded resource" in the properties under build action.您的字体必须添加到解决方案中,然后在构建操作下的属性中选择“嵌入资源”。 It looks like you may have tried to embed the font but haven't correctly named it in your stream request.看起来您可能已经尝试嵌入字体,但没有在流请求中正确命名它。 Adding your project resource collection:添加您的项目资源集合:

Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("yourApp.Eurostile_Regular_0.ttf");

You can get further information here: How to embed a True Type Font您可以在此处获取更多信息: 如何嵌入 True Type 字体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM