.net - 如何从 URL 的 Web 应用程序中的字体文件中获取字体系列?
问题描述
我必须从文件中获取字体系列。我查过了,但所有答案都在使用 WPF :. 我应该怎么办?
我FontFamily(Uri, String)
在 dot net 文档中找到了。但它在 system.windows.media
WPFPresentationCore.dll
中,在 aspnet 核心应用程序中不可用。
StackOverflow 中有另一个线程使用 glyphTypeface 但它来自 WPF 中的同一个 dll/命名空间。
我找到的最佳答案是这个,但是在 C# 中使用指针是非常可怕的。
private static string LoadFontFamilyName(byte[] buffer)
{
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
try
{
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
using var fontCollection = new PrivateFontCollection();
fontCollection.AddMemoryFont(ptr, buffer.Length);
return fontCollection.Families[0].Name;
}
finally
{
handle.Free();
}
}
有更好的想法吗?
解决方案
PrivateFontCollection
支持AddFontFile(string filename)
应该做你想做的方法。查看源代码,这似乎应该适用于 Windows 和 Unix/Linux 系统(只要后者已libgdiplus
安装)。
推荐阅读
- linux - 练习:bash 中的下载队列
- docker - 如何为构建 Docker 映像时创建的中间容器分配名称?
- javascript - 在形状内填充颜色
- python - X_train 和 y_train 是什么?
- odoo - 需要在odoo 11中更改帐户表单中的字段名称
- dart - GestureDetector onTap 卡
- android - Admob : 请求欧洲用户的同意
- postgresql - 用`ON CONFLICT DO UPDATE`替换违反两个不同约束的两行
- python - numpy 数组 - 使用 np.concatenate 或 np.insert 将零行添加到 ndarray
- code-coverage - 我的 Statement 和 Branch 覆盖范围是否正确?