Solved

WPF FontFamily from memory

Posted on 2013-01-23
5
795 Views
Last Modified: 2013-04-09
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?
0
Comment
Question by:CatsSolutions
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 38809304
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
0
 

Author Comment

by:CatsSolutions
ID: 38809835
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
0
 
LVL 9

Expert Comment

by:sognoct
ID: 38811216
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.
0
 

Author Comment

by:CatsSolutions
ID: 38812506
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.
0
 
LVL 9

Accepted Solution

by:
sognoct earned 500 total points
ID: 38813450
there is another way ... but I have not enough time for testing it. Fontfamily is not serializable with standard framework, but sharpserializer can make it serializable.

library here :
http://www.sharpserializer.com/en/index.html

so what you have to do is including sharpserializer in the service and then serialize fontfamily, so you don't have to translate it in byte but just generate a fontfamily on the server side and expose it serialized in your function.

Here is an example :
serialize fontfamily
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question