给定字体文件,如何获得字体家族的名称? [英] How do I get the name of the Font Family given the font file?

查看:323
本文介绍了给定字体文件,如何获得字体家族的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组字体文件名不可预测的字体,因此无法从文件名中推断出真正的字体家族"名称.因此,我需要读取字体元数据以提取真实的字体家族"名称,以便呈现此字体文件.我在C#.NET 4.0 WinForms中.

I have a set of font files with unpredictable filenames, so I can't deduce the real "Font Family" name from the file name. I need to therefore read the font metadata to extract the real "Font Family" name, in order to render this font file. I'm in C#.NET 4.0 WinForms.

我已经看过函数GetFontInformation,但是我似乎找不到相同的P/Invoke标头.我所拥有的只是 C ++版本,实际上很难弄清楚.有什么想法吗?

I've seen the function GetFontInformation but I can't seem to find the P/Invoke headers for the same. All I have is the C++ version which is honestly hard to figure out. Any ideas?

我无法使用PrivateFontCollection类为我解析字体文件的原因是,这些字体是 OTF 字体,.NET/GDI +仅支持 TTF 字体!

The reason I cannot use the PrivateFontCollection class to parse through a font file for me, is that these are OTF fonts and .NET/GDI+ only supports TTF fonts!

推荐答案

您需要将字体添加到(

You need to add font to the (PrivateFontCollection) and then request for the FontFamily and get its Name property.

private static string GetFontNameFromFile(string filename)
{
    PrivateFontCollection fontCollection = new PrivateFontCollection();
    fontCollection.AddFontFile("path_to_font");
    return fontCollection.Families[0].Name;
}

所需的命名空间:

using System.Drawing;
using System.Drawing.Text;

这篇关于给定字体文件,如何获得字体家族的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆