有没有办法做这样的事情?
FontFamily fontFamily = new FontFamily("C:/Projects/MyProj/free3of9.ttf");
我尝试了各种各样的变体,但却无法让它发挥作用。
更新
这有效:
PrivateFontCollection collection = new PrivateFontCollection();
collection.AddFontFile(@"C:\Projects\MyProj\free3of9.ttf");
FontFamily fontFamily = new FontFamily("Free 3 of 9", collection);
Font font = new Font(fontFamily, height);
// Use the font with DrawString, etc.
答案 0 :(得分:9)
这段代码适合我(WPF):
FontFamily fontFamily = new FontFamily(@"C:\#FONTNAME")
在您的示例中,它将是:
FontFamily fontFamily = new FontFamily(@"C:\Projects\MyProj\#free3of9");
没有文件扩展名的字体名称,并保留'#'符号
答案 1 :(得分:8)
此示例显示如何从字节数组添加字体 - 如果字体存储在资源中。它也允许从文件中添加字体。以下代码我在winforms上使用:
这有点棘手,从文件中加载TTF字体需要这样做:
private PrivateFontCollection _privateFontCollection = new PrivateFontCollection();
public FontFamily GetFontFamilyByName(string name)
{
return _privateFontCollection.Families.FirstOrDefault(x => x.Name == name);
}
public void AddFont(string fullFileName)
{
AddFont(File.ReadAllBytes(fullFileName));
}
public void AddFont(byte[] fontBytes)
{
var handle = GCHandle.Alloc(fontBytes, GCHandleType.Pinned);
IntPtr pointer = handle.AddrOfPinnedObject();
try
{
_privateFontCollection.AddMemoryFont(pointer, fontBytes.Length);
}
finally
{
handle.Free();
}
}