Link to home
Start Free TrialLog in
Avatar of CatsSolutions
CatsSolutions

asked on

WPF FontFamily from memory

HI,

I am creating a program that downloads a required font through a web service as a byte[] and I would like to assign the font on a label control.

I know I could save the font as a file and get a new FontFamily from a URI but I'd rather not have temporary files.

I have seen that in System.Drawing you could use a PrivateFontCollection from memory but I'm not sure if its possible use that in a WPF label.

Any help?
Avatar of sognoct
sognoct
Flag of Italy image

have not tested on WPF but as I read in some forum should be the same of standard framework. Try if this snippet works on your project

suppose that fontData is Byte[]
and fondatalenght its lenght

            PrivateFontCollection pfc = new PrivateFontCollection();
            IntPtr data = Marshal.AllocCoTaskMem(fondatalenght);
            uint cFonts = 0;
            AddFontMemResourceEx(data, (uint)fontData.Length, IntPtr.Zero, ref cFonts);
            pfc.AddMemoryFont(data, fondatalenght);
            label1.Font = New Font(pfc.Families(0), 10)
            label1.UseCompatibleTextRendering = True

Open in new window


this code comes from a project of mine, I've done some modification on the fly, so some debugging is necessary
Avatar of CatsSolutions
CatsSolutions

ASKER

Hi sognot,

Thank you for the reply but the code does not seem to work. A label has a fontfamily, not a font.

Where does AddFontMemResourceEx come from?

Label does not have a property for UseCompatibleTextRendering
sorry now I tested code, wpf does not allow PrivateFontCollection as in standard forms.
The only and simplest way is to put the font inside the site that host web service then you can call :

label1.FontFamily = new FontFamily( new Uri("http://myserver/Fonts/#My Custom Font", UriKind.Absolute), "My Custom Font");

So service can expose the fonts not as byte but as url of the disponible font to use.
Unfortunately that's something I can't do so I think I'll have to settle for creating temporary files and deleting them when done.
ASKER CERTIFIED SOLUTION
Avatar of sognoct
sognoct
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial