Integration method

suggest change

AddMemoryFont keeps using the block you allocate, and the FontFamily stays valid only while that PrivateFontCollection is alive. Keep both for as long as you draw with the font. Freeing the block here, or dropping the collection, makes the family unusable.

private static readonly PrivateFontCollection Fonts = new PrivateFontCollection();

public static FontFamily GetResourceFontFamily(byte[] fontbytes)
{
    IntPtr fontMemPointer = Marshal.AllocCoTaskMem(fontbytes.Length);
    Marshal.Copy(fontbytes, 0, fontMemPointer, fontbytes.Length);
    Fonts.AddMemoryFont(fontMemPointer, fontbytes.Length);
    return Fonts.Families[Fonts.Families.Length - 1];
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents