2009-04-07 27 views
15

在我正在開發的應用程序中嵌入truetype字體的最佳方式是什麼?基本上我想確保一個特定的字體可用於我的應用程序安裝在另一臺機器上。我有* .ttf字體文件,只需要一種嵌入方式或在應用程序運行時自動安裝它。如何使用C#應用程序嵌入字體? (使用Visual Studio 2005)

是否需要設置安裝程序以在安裝期間安裝字體,還是可以在應用程序運行時動態加載字體?事實上,兩人都很高興知道。

該應用程序正在使用.NET 2.0在C#中開發。

回答

11

它似乎比這更容易;您可以將字體作爲資源嵌入到您的應用中,並作爲應用的「屬性」命名空間中的強類型屬性進行訪問。但是,給定的鏈接應該是一個很好的起點。


對於VB-禁用:

Add the font as a resource in your application.調用資源MyFontLol。您可以從Properties.Resources.MyFontLol訪問此資源(作爲字節數組)。

我沒有測試以下,但它似乎是可行的:

public void LoadMyFontLolKThx() 
{ 
    // get our font and wrap it in a memory stream 
    byte[] myFont = Properties.Resources.MyFontLol; 
    using (var ms = new MemoryStream(myFont)) 
    { 
     // used to store our font and make it available in our app 
     PrivateFontCollection pfc = new PrivateFontCollection(); 
     // The next call requires a pointer to our memory font 
     // I'm doing it this way; not sure what best practice is 
     GCHandle handle = GCHandle.Alloc(ms, GCHandleType.Pinned); 
     // If Length > int.MaxValue this will throw 
     checked 
     { 
      pfc.AddMemoryFont(
       handle.AddrOfPinnedObject(), (int)ms.Length); 
     } 
     var font = new Font(pfc.Families[0],12); 

     // use your font here 
    } 
} 

一個最後一個音符。 PFC將字體存儲爲GDI +字體。這些與某些表單控件不兼容。從文檔:

要使用內存字體,必須使用GDI +呈現 控件上的文本。 使用 SetCompatibleTextRenderingDefault 方法,傳遞真實,設置應用GDI + 渲染,或由 控制的UseCompatibleTextRendering 屬性設置爲true 單獨控制。一些控件不能用GDI +渲染。

+0

我的。字體。大聲笑。 _that_是你能想到的最好的名字嗎? – 2014-04-16 21:32:46

+0

我可以挖掘方法名稱LolKThx – 2014-08-26 16:17:53

+0

當我嘗試這樣做時,將MemoryStream傳遞給GCHandle.Alloc肯定有問題 - 不是基本類型對象是異常的要點。 *聳聳肩* – 2015-04-24 20:57:54

12

以下是具體的回答,翻譯成C#(未經測試):

PrivateFontCollection pfc = new PrivateFontCollection(); 

using (Stream fontStream = GetType().Assembly.GetManifestResourceStream("Alphd___.ttf")) 
{ 
    if (null == fontStream) 
    { 
     return; 
    } 

    int fontStreamLength = (int) fontStream.Length; 

    IntPtr data = Marshal.AllocCoTaskMem(fontStreamLength); 

    byte[] fontData = new byte[fontStreamLength]; 
    fontStream.Read(fontData, 0, fontStreamLength); 

    Marshal.Copy(fontData, 0, data, fontStreamLength); 

    pfc.AddMemoryFont(data, fontStreamLength); 

    Marshal.FreeCoTaskMem(data); 
} 

與他們的油漆沿()方法:

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

    e.Graphics.PageUnit = GraphicsUnit.Point; 

    using (SolidBrush b = new SolidBrush(Color.Black)) 
    { 
     int y = 5; 

     foreach (FontFamily fontFamily in pfc.Families) 
     { 
      if (fontFamily.IsStyleAvailable(FontStyle.Regular)) 
      { 
       using (Font font = new Font(fontFamily, 32, FontStyle.Regular)) 
       { 
        e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); 
       } 
       y += 40; 
      } 
      if (fontFamily.IsStyleAvailable(FontStyle.Bold)) 
      { 
       bold = true; 
       using (Font font = new Font(fontFamily, 32, FontStyle.Bold)) 
       { 
        e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); 
       } 
       y += 40; 
      } 
      if (fontFamily.IsStyleAvailable(FontStyle.Italic)) 
      { 
       italic = true; 
       using (Font font = new Font(fontFamily, 32, FontStyle.Italic)) 
       { 
        e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); 
       } 
       y += 40; 
      } 

      if(bold && italic) 
      { 
       using(Font font = new Font(fontFamily, 32, FontStyle.Bold | FontStyle.Italic)) 
       { 
        e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); 
       } 
       y += 40; 
      } 

      using (Font font = new Font(fontFamily, 32, FontStyle.Underline)) 
      { 
       e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); 
       y += 40; 
      } 

      using (Font font = new Font(fontFamily, 32, FontStyle.Strikeout)) 
      { 
       e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); 
      } 
     } 
    } 
} 
-2

它可能不是最好的辦法,但你能不能只包括字體與您的資源,然後將其複製到Windows dir的字體文件夾?

相關問題