Solved

WPF FontFamily from memory

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DataGridview Currency Formating? 22 38
Make a border less form movable 2 12
VB.NET HttpWebRequest 12 32
Saveas need to save a copy 16 11
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

21 Experts available now in Live!

Get 1:1 Help Now