C# 嵌入式字体可正确安装并通过图形绘制,但不能在Windows窗体控件中使用
我已经使用C# 嵌入式字体可正确安装并通过图形绘制,但不能在Windows窗体控件中使用,c#,.net,fonts,C#,.net,Fonts,我已经使用PrivateFontCollection在我的应用程序中嵌入了字体。我可以安装字体,然后在图形对象中完美地使用它们。问题是在我的表单控件font属性中无法识别字体名称。我想确保如果我创建了一个Font对象,比如新字体(“MyFontName”,…)名称MyFontName被识别并使用加载的字体 我所做的: public FontFamily InstallFromMemory(byte[] bytes) { // Get unsafe bytes GCHandle p
PrivateFontCollection
在我的应用程序中嵌入了字体。我可以安装字体,然后在图形
对象中完美地使用它们。问题是在我的表单控件font
属性中无法识别字体名称。我想确保如果我创建了一个Font
对象,比如新字体(“MyFontName”,…)
名称MyFontName
被识别并使用加载的字体
我所做的:
public FontFamily InstallFromMemory(byte[] bytes)
{
// Get unsafe bytes
GCHandle pinnedArray = GCHandle.Alloc(bytes, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();
// install for gdi+
Collection.AddMemoryFont(pointer, bytes.Length);
var result = Collection.Families[Collection.Families.Length - 1];
// install for gdi
// do the unmanaged font (Windows 2000 and later)
uint cFonts;
var r = AddFontMemResourceEx(bytes, bytes.Length, IntPtr.Zero, out cFonts);
if (r.ToInt64() == 0)
throw new Exception("Failed to install the font.");
// free unsafe bytes
pinnedArray.Free();
Families.Add(result.Name, result);
return result;
}
它没有错误,但也不起作用。
AddFontMemResourceEx
是否是要调用的正确API函数?我应该怎么做才能得到预期的结果?标准错误,在程序无法再使用字体并且您调用了PrivateFontCollection.Dispose()之前,您不能调用Free()。这使得使用GCHandle.Alloc()非常糟糕,请改用Marshal.allocTaskMem并复制数组数据。这使得这个类非常笨重,使用它的唯一体面的方法是在前面创建字体,而不是麻烦再次发布它们。当你的程序结束时,一切都消失了。@HansPassant:你是说我调用了pinnerArray.Free()?因为我根本没有处理PrivateFontCollection.Standard bug,所以在您的程序无法再使用字体并且您已调用PrivateFontCollection.dispose()之前,您无法调用Free()。这使得使用GCHandle.Alloc()非常糟糕,请改用Marshal.allocTaskMem并复制数组数据。这使得这个类非常笨重,使用它的唯一体面的方法是在前面创建字体,而不是麻烦再次发布它们。当你的程序结束时,一切都消失了。@HansPassant:你是说我调用了pinnerArray.Free()?因为我根本没有处理PrivateFontCollection。