Sep 1, 2010

Embedding Font To Resources

How to load and use custom font, embedded in assembly resources.

We leared how to load font not installed in system. But what about embedding font into assembly resources? We will use PrivateFontCollection as in previous example private font loading. But we'll need little bit more action.

First we add font file into project. Place it in root folder of project. Go to property and choose action Embedded Resource for font file.

Next add code to read font from resource.


// specify embedded resource name
string resource = "embedded_font.PAGAP___.TTF";

// receive resource stream
Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);

// create an unsafe memory block for the font data
System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);

// create a buffer to read in to
byte[] fontdata = new byte[fontStream.Length];

// read the font data from the resource
fontStream.Read(fontdata, 0, (int)fontStream.Length);

// copy the bytes to the unsafe memory block
Marshal.Copy(fontdata, 0, data, (int)fontStream.Length);

// pass the font to the font collection
private_fonts.AddMemoryFont(data, (int)fontStream.Length);

// close the resource stream
fontStream.Close();

// free up the unsafe memory
Marshal.FreeCoTaskMem(data);

After that we can create font and assign font to label

label1.Font = new Font(private_fonts.Families[0], 22);

Unfortunately it will not work instead loaded font from file. Reason is specific of memory loaded fonts, described in remarks to AddMemoryFont method.
To use the memory font, text on a control must be rendered with GDI+. Use the SetCompatibleTextRenderingDefault method, passing true, to set GDI+ rendering on the application, or on individual controls by setting the control's UseCompatibleTextRendering property to true.
You can specify in Program class folowing:

Application.SetCompatibleTextRenderingDefault(true);

But it can affect other controls in programm. As example some controls fonts can look ugly. So better specify GDI+ rendering only for choosen controls.

label1.UseCompatibleTextRendering = true;

Result:

Download sample project.

Today's reading: Pro .NET 2.0 Graphics Programming (Expert's Voice in .NET)

No comments:

Post a Comment