Click here to Skip to main content
16,016,669 members
Articles / Desktop Programming / Windows Forms

Embedding Font To Resources

Rate me:
Please Sign up or sign in to vote.
4.93/5 (10 votes)
7 Sep 2010CPOL 74.2K   23   1
How to load and use custom font, embedded in assembly resources

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.

C#
// 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:

C#
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:

C#
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.

C#
label1.UseCompatibleTextRendering = true;

Result

This article was originally posted at http://c-sharpening.blogspot.com/feeds/posts/default

License

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


Written By
Software Developer (Senior) i-BLADES
Thailand Thailand
I'm Android and Full Stack Software Engineer. 28 years in software industry, lots of finished projects. I’m never afraid of learning something new and you can see it by amount of skills in my resume.

I'm working remotely since 2009, self-motivated and self-organized.

There are no impossible projects. I have experience with Android, iOS, Web, Desktop, Embedded applications, VR, AR, XR, Computer vision, Neural networks, Games, IoT, you name it.

Comments and Discussions

 
QuestionDoesn't work on TextBox, ComboBox, etc Pin
microvb14-Oct-15 1:52
microvb14-Oct-15 1:52 
Thank you for your submission, however this is only valid for controls that can not potentially be typed in, like Label, or Form. Some controls this does not work with are as follows:

ListView
ComboBox
ListBox
TextBox
RichTextBox

Could you please expand this to show how those controls can work with a font loaded from resources.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.