1
votes

I want a specific font to display on certain buttons in an application so have chosen to include it in the project as a resource.

I then .addMemoryFont to a new PrivateFontCollection by the way of the Marshal namespace and then set the font to my new font family.

This works on Windows Vista, Windows7 and Windows 8 (preview) but will not work on windows XP which my app has to support.

There is no error on XP .. the font just doesn't display.

I have also tried embedding the font as a file and loading it through a stream object - Assembly.GetExecutingAssembly().GetManifestResourceStream(resource)

This again works on everything but XP, any thoughts? Thanks

Here's the function that I load the resource with: For this testing I have added AVP.TTF (widely available, non commercial) as a resource called AVP.

I use button.Font = LoadFontResource(9.75, FontStyle.Regular) in the client.

Public Function LoadFontResource(ByVal Size As Single, ByVal style As FontStyle) As Font
    _fontsResource = New PrivateFontCollection
    Dim fontMemPointer As IntPtr = Marshal.AllocCoTaskMem(My.Resources.AVP.Length)
    Marshal.Copy(My.Resources.AVP, 0, fontMemPointer, My.Resources.AVP.Length)
    _fontsResource.AddMemoryFont(fontMemPointer, My.Resources.AVP.Length)
    Marshal.FreeCoTaskMem(fontMemPointer)
    Return New Font(_fontsResource.Families(0), Size, style)
End Function
1
Post code that reproduces the problem and a link to the trouble font. - Hans Passant
Why do you need to support Windows XP. In less then 17 months it won't even be supported by Microsoft. - Security Hound
@Ramhound, There are plenty of reasons to support XP. It's not exactly a fringe need. - Brad
@Ramhound: OS support would have nothing to do with people buying my clients software. - Sunrise
Do not release the memory until you are sure that the font will no longer be used. - Hans Passant

1 Answers

0
votes

Writing the embedded font to a temporary file and then using .AddFontFile leads to a more stable performance in XP. Thanks Hans for pointing me down the old GDI+ route.