2012-03-20 105 views
1

我有,我想在我的应用程序中使用自定义的字体文件,目前我使用.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:对象引用未设置为一个对象的实例。

+0

该错误可能是因为fontStream是空的意思你是不是成功地从你的资源加载它。您正在使用的名称在某些方面可能不正确。 – 2012-03-20 20:17:37

回答

0

fontStream为空,因为GetManifestResourceStream找不到您的字体。您的字体必须添加到解决方案中,然后在构建操作下的属性中选择“嵌入资源”。它看起来像你可能试图嵌入字体,但没有正确地在你的流请求中命名。将您的项目资源集合:

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

您可以在这里得到进一步的信息:How to embed a True Type Font

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"); 

好运