AddFontMemResourceEx (gdi32)
Last changed: -2.147.187.117

.
Summary
Adds the font resource from a memory image to the system.

C# Signature:

[DllImport("gdi32.dll", ExactSpelling=true)]

private static extern IntPtr AddFontMemResourceEx(byte[] font, int fontLength, IntPtr reserved>Null, out uint installedCount);

User-Defined Types:

None.

Notes:

When the function succeeds, the caller of this function can free the memory pointed to by pbFont because the system has made its own copy of the memory. To remove the fonts that were installed, call RemoveFontMemResourceEx. However, when the process goes away, the system will unload the fonts even if the process did not call RemoveFontMemResourceEx.

Tips & Tricks:

Calling this API is required when working with PrivateFontCollection and AddMemoryFont, or you won't get it working.

Sample Code:

PrivateFontCollection privateFontCollection = new PrivateFontCollection();

byte[] fontdata = MyAssembly.Properties.Resources.ZeroThreesFont; // Getting Zero Threes embedded font from Resources (.NET 2.0)

IntPtr ptrFont = Marshal.AllocCoTaskMem(fontdata.Length);

uint cFonts;

AddFontMemResourceEx(fontdata, fontdata.Length, IntPtr.Zero, out cFonts);

Marshal.Copy(fontdata, 0, ptrFont, fontdata.Length);

privateFontCollection.AddMemoryFont(ptrFont, fontdata.Length);

Marshal.FreeCoTaskMem(ptrFont);

Alternative Managed API:

Do you know one? Please contribute it!

Documentation