2010-03-23 50 views

回答

1

這是我以前用過的,這是用於web應用程序,所以可能不完全是你想要的。字體ttf文件也被存儲在數據庫中。您將需要用實際字節[]替換[FONTASBYTEARRAY]。

將ttf文件轉換爲字體對象可能有更好的方法,但這會讓你開始。

using System; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Drawing.Text; 
using System.Runtime.InteropServices; 

namespace Utility 
{ 
    public class Font 
    { 
     public string GetFont(byte[] [FONTASBYTEARRAY]) 
     { 
      PrivateFontCollection fc = new PrivateFontCollection(); 
      IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement([FONTASBYTEARRAY], 0); 
      fc.AddMemoryFont(pointer, Convert.ToInt32([FONTASBYTEARRAY].Length)); 
      System.Drawing.Font f = new System.Drawing.Font(fc.Families[0], 10); 
      FontFamily ff = f.FontFamily; 
      return ff.Name; 
     } 
    } 
} 
2

這是最容易被導入System.Windows.Media命名空間,它給你多與工作,比得到的字體出來的ByteArray的簡單API來完成

using System.Windows.Media; 

String fontFilePath = "PATH TO YOUR FONT"; 
GlyphTypeface glyphTypeface = new GlyphTypeface(fontFileURI); 
String fontFamily = glyphTypeface.Win32FamilyNames[new System.Globalization.CultureInfo("en-us")]; 
String fontFace = glyphTypeface.Win32FaceNames[new System.Globalization.CultureInfo("en-us")]; 

Console.WriteLine("Font: " + fontFamily + " " + fontFace); 
+0

你可以找到許多示例都希望Registry能夠做到這一點,但您的解決方案是更清晰的解決方案。好。非常感謝,真的很有幫助。 – Fabio 2013-04-05 08:56:47