4
我有一個.ttf文件,我想要檢索字體系列名稱。字體文件中的字體家族名稱
我有一個.ttf文件,我想要檢索字體系列名稱。字體文件中的字體家族名稱
這是我以前用過的,這是用於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;
}
}
}
這是最容易被導入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);
你可以找到許多示例都希望Registry能夠做到這一點,但您的解決方案是更清晰的解決方案。好。非常感謝,真的很有幫助。 – Fabio 2013-04-05 08:56:47