【发布时间】:2011-05-25 12:42:14
【问题描述】:
我正在从文件系统加载字体。
通常我的代码工作得很好,但是当我通过 PrivateFontCollection 加载字体时,加载的 FontFamily 的名称是字体中字体的名称(我可以在 windows 的字体预览器中的字体名称下看到)和不是 FontFamily 名称...
因为我有一些字体具有相同的字体名称但另一个字体名称我希望能够区分它们。任何人都知道如何获得真正的 FontFamily 名称?
public Dictionary<string, FontFamily> LoadFontsFromDirectory(string path)
{
Dictionary<string, FontFamily> foundFonts = new Dictionary<string, FontFamily>();
if (!Directory.Exists(path)) throw new Exception("directory doesnt exist");
foreach(FileInfo fi in new DirectoryInfo(path).GetFiles("*.ttf"))
{
PrivateFontCollection fileFonts = new PrivateFontCollection();
fileFonts.AddFontFile(fi.FullName);
if (!foundFonts.ContainsKey(fileFonts.Families[0].Name))
{
//add the font only if this fontfamily doesnt exist yet
FontFamily family = new FontFamily(String.Format("file:///{0}#{1}", fi.FullName, fileFonts.Families[0].Name));
foundFonts.Add(family.Name, family);
}
}
return foundFonts;
}
【问题讨论】:
-
这个链接对我有用吗? MSDN
-
抱歉,我的意思是那个链接对你有用吗?
-
其实,略过。我应该花更多时间阅读这个问题,链接不是你要找的。我的道歉
-
您的意思是要区分 Times New Roman 和 Times New Roman Bold(例如)?在这种情况下,两个家族都是 Times New Roman。