C# WebAPI使用PrivateFontCollection无法添加字体-有时
我得到了需要在WebAPI应用程序中呈现的项目列表。这些使用我在文件存储中存储的ttf字体 我正在尝试迭代需要渲染的组件,对于我获得的每个字体Id,下载该字体,并将其添加到C# WebAPI使用PrivateFontCollection无法添加字体-有时,c#,asp.net-core,fonts,asp.net-core-webapi,privatefontcollection,C#,Asp.net Core,Fonts,Asp.net Core Webapi,Privatefontcollection,我得到了需要在WebAPI应用程序中呈现的项目列表。这些使用我在文件存储中存储的ttf字体 我正在尝试迭代需要渲染的组件,对于我获得的每个字体Id,下载该字体,并将其添加到PrivateFontCollection,我已为我的类声明为静态 private static PrivateFontCollection pfc=new PrivateFontCollection() 因此,当我收到我的组件时,我有一个方法将字体加载到集合中。如果还没有字体,我就添加它 我首先展示了状态。启动时-除此之外,
PrivateFontCollection
,我已为我的类声明为静态
private static PrivateFontCollection pfc=new PrivateFontCollection()代码>
因此,当我收到我的组件时,我有一个方法将字体加载到集合中。如果还没有字体,我就添加它
我首先展示了状态。启动时-除此之外,它是空的。
我在集合中查找与我的组件链接的姓氏的字体。
如果我找到它,我将继续下一个组件。如果没有,则使用Id调用一个服务,该服务将下载字体的字节[]
然后我将字体添加到集合中。服务提供的数据很好。我有一个字节[],大小很完美
50%的情况下,这是有效的。然而,有时,我也不知道为什么,行pfc.AddMemoryFont(ptrData,dataLength)代码>执行时没有错误,但计数前和计数后是相同的。字体没有添加到集合中-我没有收到任何错误
注:我使用相同的数据进行测试
private async Task LoadFonts(List<PlateDataObj> components)
{
_log.LogDebug("LoadFonts called... Current State:");
foreach (var item in pfc.Families)
{
_log.LogDebug("PFC - Name: {name}", item.Name);
}
foreach(var component in components)
{
var found = pfc.Families.FirstOrDefault(x => x.Name == component.GroupMeta.FontFamilyName);
if(found == null)
{
_log.LogDebug("Font family {family} not found. Will add.", component.GroupMeta.FontFamilyName);
var f = await _data.GetFont(component.GroupMeta.FontTypeId);
var fontUrl = _blobManager.GetPath(ImageTypeEnum.Font) + f.Url;
var fontData = await _blobManager.Download(fontUrl);
int dataLength = fontData.FileData.Length;
IntPtr ptrData = Marshal.AllocCoTaskMem(dataLength);
Marshal.Copy(fontData.FileData, 0, ptrData, dataLength);
_log.LogDebug("Before adding... Count is {count}", pfc.Families.Count());
pfc.AddMemoryFont(ptrData, dataLength);
_log.LogDebug("After adding... Count is {count}", pfc.Families.Count());
fontMapper.Add(new FontMapper
{
FamilyName = component.GroupMeta.FontFamilyName,
Id = component.GroupMeta.FontTypeId
});
Marshal.FreeCoTaskMem(ptrData);
} else
{
_log.LogDebug("Font family {family} exist. No need to add.", component.GroupMeta.FontFamilyName);
}
}
_log.LogDebug("LoadFonts Complete... Current State:");
foreach (var item in pfc.Families)
{
_log.LogDebug("PFC - Name: {name}", item.Name);
}
}
如您所见,添加了第一种字体,但第二种。。。count()保持不变(使用断点验证)
我会运行这几次,有时计数是正确的,字体会增加-其他的,不会。有人能发现一个明显的问题吗?如果您仍然需要使用pfc
中的字体,那么您不应该通过调用Marshal.FreeCoTaskMem(ptrData)来损坏他们的数据代码>。只需调用pfc.Dispose()代码>当集合中的字体不再需要时。
[12:25:40.720 DBG] LoadFonts called... Current State:
[12:25:45.371 DBG] Font family BigHonk handwriting not found. Will add.
[12:25:45.707 DBG] GetFont: Loading Font Id: 1
[12:25:48.123 DBG] GetFont: Complete in 2414ms
[12:25:49.493 DBG] [Download] Request to download image at Url: [https://example.com/fonts/737762b2-2859-419b-8752-1f8bb2e03f5d.ttf]
[12:25:49.834 DBG] [Download] Request to download completed in 323ms
[12:25:57.375 DBG] Before adding... Count is 0
[12:25:58.862 DBG] After adding... Count is 1
[12:26:09.699 DBG] Font family BigHonk handwriting not found. Will add.
[12:26:09.703 DBG] GetFont: Loading Font Id: 1
[12:26:09.749 DBG] GetFont: Complete in 45ms
[12:26:09.750 DBG] [Download] Request to download image at Url: [https://example.com/fonts/737762b2-2859-419b-8752-1f8bb2e03f5d.ttf]
[12:26:10.004 DBG] [Download] Request to download completed in 253ms
[12:26:10.006 DBG] Before adding... Count is 1
[12:26:13.599 DBG] After adding... Count is 1