2009-02-17 72 views
22

這可能是小白的領土,但到底是什麼:如何快速,輕鬆地嵌入在WinForms應用程序的字體在C#

我想嵌入字體在我的WinForms應用程序,這樣我就不必擔心它們安裝在機器上。我在MSDN網站上搜索了一下,發現了一些關於使用本地Windows API調用的提示,例如Scott Hanselman鏈接的Michael Caplans(sp?)教程。現在,我真的必須經歷所有這些麻煩嗎?我不能只使用我的應用程序的資源部分?

如果沒有,我可能會去安裝路線。在那種情況下,我可以通過編程來做到嗎?通過將字體文件複製到Windows \ Fonts文件夾?

編輯:我知道授權問題,謝謝你的關切。

+2

您可以這樣做,但也要注意,類似的軟件字體也有許可證,您需要確保您沒有違反任何禁止嵌入和部署的許可證。 – Rad 2009-02-17 10:07:44

回答

1

我不能只使用我的應用程序的資源部分?

是的,但需要本地資源而不是.NET資源(即使用rc.exe,本地資源編譯器)。

4
// specify embedded resource name 
string resource = "embedded_font.PAGAP___.TTF"; 

// receive resource stream 
Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource); 

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

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

// read the font data from the resource 
fontStream.Read(fontdata, 0, (int)fontStream.Length); 

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

// pass the font to the font collection 
private_fonts.AddMemoryFont(data, (int)fontStream.Length); 

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

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

這是我在VS 2013中的工作,而不必使用不安全的塊。

嵌入資源

  1. 雙擊Resources.resx,並在工具欄的設計師單擊添加資源/添加現有文件,並選擇您的.ttf文件
  2. 在您的解決方案資源管理器中,右鍵單擊您的.ttf文件(現在位於資源文件夾中)並轉到屬性。設置爲「嵌入的資源」

加載字體到內存

  1. 添加using System.Drawing.Text;您Form1.cs的文件

  2. 添加以上代碼和默認構造函數中生成操作在內存中創建字體(不像其他示例中所示的那樣使用「不安全」)。下面是我的整個Form1.cs中:

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    using System.Reflection; 
    
    using System.Drawing.Text; 
    
    namespace WindowsFormsApplication1 
    { 
        public partial class Form1 : Form 
        { 
         [System.Runtime.InteropServices.DllImport("gdi32.dll")] 
         private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, 
          IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts); 
    
         private PrivateFontCollection fonts = new PrivateFontCollection(); 
    
         Font myFont; 
    
         public Form1() 
         { 
          InitializeComponent(); 
    
          byte[] fontData = Properties.Resources.MyFontName; 
          IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length); 
          System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length); 
          uint dummy = 0; 
          fonts.AddMemoryFont(fontPtr, Properties.Resources.MyFontName.Length); 
          AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.MyFontName.Length, IntPtr.Zero, ref dummy); 
          System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr); 
    
          myFont = new Font(fonts.Families[0], 16.0F); 
         } 
        } 
    } 
    

使用您的字體

  1. 標籤添加到您的主要形式,並添加加載事件來設置Form1中的字體。 CS:

    private void Form1_Load(object sender, EventArgs e) 
    { 
        label1.Font = myFont; 
    } 
    
+0

這對我不起作用。我的字體不出現在Properties.Resources下。 – 2015-02-06 12:15:00

0

我拖並將randomly downloaded font放入我的資源中,並且工作正常。儘管使用文件。我想你也可以使用它來安裝字體?

public Form1() 
{ 
    string filename = @"C:\lady.gaga"; 
    File.WriteAllBytes(filename, Resources.KBREINDEERGAMES); 
    PrivateFontCollection pfc = new PrivateFontCollection(); 
    pfc.AddFontFile(filename); 

    Label label = new Label(); 
    label.AutoSize = true; 
    label.Font = new Font(pfc.Families[0], 16); 
    label.Text = "hello world"; 
    Controls.Add(label); 
} 
相關問題