4

我有一个 .ttf 文件,我想检索字体系列名称。

4

2 回答 2

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);
于 2012-08-24T15:20:19.013 回答
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;
        }
    }
}
于 2010-03-23T05:36:29.657 回答