Click here to Skip to main content
Click here to Skip to main content

Embedding Font To Resources

By , 7 Sep 2010
Rate this:
Please Sign up or sign in to vote.

Introduction

This post discusses how to load and use custom font, embedded in assembly resources.

Solution

We will use PrivateFontCollection as in the previous example private font loading. But we'll need a little bit more action.

Add Font to Resource

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

Load Font from Resource

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);

Problems and Workaround

Unfortunately, it will not work instead loaded font from file. The reason is specific to 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 as follows:

Application.SetCompatibleTextRenderingDefault(true);

But it can affect other controls in the program. As an example, some controls fonts can look ugly. So better specify GDI+ rendering only for chosen controls.

label1.UseCompatibleTextRendering = true;

Result

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ernest Poletaev
Software Developer (Senior) Enterprise Information Systems, Plc
Thailand Thailand
Senior Software Developer in Enterprise Information Systems, an ERP / CRM / Docflow Software Solution Provider for Russian Local Market.
 
Professional Developer (C, C++, C#), since 1984. Experienced in many programming technologies.
 
Now i resides in Kalasin, Thailand, working remotely for my company

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 7 Sep 2010
Article Copyright 2010 by Ernest Poletaev
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid