【发布时间】:2010-03-23 05:23:33
【问题描述】:
我有一个 .ttf 文件,我想检索字体系列名称。
【问题讨论】:
标签: c# font-family
我有一个 .ttf 文件,我想检索字体系列名称。
【问题讨论】:
标签: c# font-family
这很容易通过导入 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);
【讨论】:
这是我过去使用的,这是用于 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;
}
}
}
【讨论】: