使用自定义字体问题

时间:2012-03-20 19:02:59

标签: c# winforms fonts

我有一个自定义字体文件,我想在我的应用程序上使用,目前我正在使用.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);
}

在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();
}

但字体无法正常工作,我从form_load

的第二行收到JIT错误
  

System.NullReferenceException:未将对象引用设置为对象的实例。

2 个答案:

答案 0 :(得分:1)

首先转到Font Properties - > Build Action - > "Embedded Resource"

第二个问题是您忘记在代码中添加NameSpace名称

格式错误

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

格式正确

Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("NameSpaceName.FileName.ttf");
祝你好运

答案 1 :(得分:0)

fontStream为null,因为GetManifestResourceStream无法找到您的字体。您的字体必须添加到解决方案中,然后在构建操作下的属性中选择“嵌入资源”。看起来您可能已尝试嵌入字体但未在流请求中正确命名。添加项目资源集合:

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

您可以在此处获取更多信息:How to embed a True Type Font