Solved

WPF FontFamily from memory

Posted on 2013-01-23
5
804 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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