嵌入式字体安装并由Graphics正确绘制,但不能在Windows窗体控件中使用

时间:2016-03-07 16:07:05

标签: c# .net fonts

我使用PrivateFontCollection在我的应用中嵌入了字体。我可以安装字体,然后在Graphics对象中完美地使用它们。问题是字体名称在我的表单控件Font属性中无法识别。我想确保如果我创建一个Font对象,例如new 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函数也可以调用吗?我该怎么做才能得到我想要的结果?

0 个答案:

没有答案
相关问题