Solved

WPF FontFamily from memory

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now