How to Embed My Own Fonts in a Winforms App

Using custom fonts on a Label on Winforms

Embed the font as a resource (or just include it in the bin directory), and then use the PrivateFontCollection to load the font (See the AddFontFile and AddMemoryFont functions). You then use the font normally like it was installed on the machine.

The PrivateFontCollection class allows
applications to install a private
version of an existing font without
the requirement to replace the system
version of the font. For example, GDI+
can create a private version of the
Arial font in addition to the Arial
font that the system uses.
PrivateFontCollection can also be used
to install fonts that do not exist in
the operating system.

Source

How do I Embed a font with my C# application? (using Visual Studio 2005)

This blog post should help you.

Basically you add the font as an embedded resource then load it into a PrivateFontCollection object.

Embedding/deploying custom font in .NET app

I use a custom font for my custom graphics-library on an asp.net site, but this should also work on winform without issues. You just specify the font-file, the font-size and the font-style, and the font-type is returned.

public static LoadedFont LoadFont(FileInfo file, int fontSize, FontStyle fontStyle)
{
var fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(file.FullName);
if (fontCollection.Families.Length < 0)
{
throw new InvalidOperationException("No font familiy found when loading font");
}

var loadedFont = new LoadedFont();
loadedFont.FontFamily = fontCollection.Families[0];
loadedFont.Font = new Font(loadedFont.FontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
return loadedFont;
}

LoadedFont is a simple struct

public struct LoadedFont
{
public Font Font { get; set; }
public FontFamily FontFamily { get; set; }
}

This is needed to prevent the FontFamily to be GC'ed and the font not working (asp.net, i do not know if it is needed on winform).

Custom .ttf fonts to use in C# windows.Form

This article: How to embed a true type font shows how to do what you ask in .NET.

How to embed a True Type font

Some applications, for reasons of esthetics or a required visual
style, will embed certain uncommon fonts so that they are always there
when needed regardless of whether the font is actually installed on
the destination system.

The secret to this is twofold. First the font needs to be placed in
the resources by adding it to the solution and marking it as an
embedded resource. Secondly, at runtime, the font is loaded via a
stream and stored in a PrivateFontCollection object for later use.

This example uses a font which is unlikely to be installed on your
system. Alpha Dance is a free True Type font that is available from
the Free Fonts Collection. This font was embedded into the application
by adding it to the solution and selecting the "embedded resource"
build action in the properties.

Figure 1. The Alpha Dance font file embedded in the solution.

Once the file has been successfully included in the resources you need
to provide a PrivateFontCollection object in which to store it and a
method by which it's loaded into the collection. The best place to do
this is probably the form load override or event handler. The
following listing shows the process. Note how the AddMemoryFont method
is used. It requires a pointer to the memory in which the font is
saved as an array of bytes. In C# we can use the unsafe keyword for
convienience but VB must use the capabilities of the Marshal classes
unmanaged memory handling. The latter option is of course open to C#
programmers who just don't like the unsafe keyword.
PrivateFontCollection pfc = new PrivateFontCollection();

private void Form1_Load(object sender, System.EventArgs e)
{
Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("embedfont.Alphd___.ttf");

byte[] fontdata = new byte[fontStream.Length];
fontStream.Read(fontdata,0,(int)fontStream.Length);
fontStream.Close();
unsafe
{
fixed(byte * pFontData = fontdata)
{
pfc.AddMemoryFont((System.IntPtr)pFontData,fontdata.Length);
}
}
}

Fonts may have only certain styles which are available and
unfortunately, selecting a font style that doesn't exist will throw an
exception. To overcome this the font can be interrogated to see which
styles are available and only those provided by the font can be used.
The following listing demonstrates how the Alpha Dance font is used by
checking the available font styles and showing all those that exist.
Note that the underline and strikethrough styles are pseudo styles
constructed by the font rendering engine and are not actually provided
in glyph form.

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
bool bold=false;
bool regular=false;
bool italic=false;

e.Graphics.PageUnit=GraphicsUnit.Point;
SolidBrush b = new SolidBrush(Color.Black);

float y=5;

System.Drawing.Font fn;

foreach(FontFamily ff in pfc.Families)
{
if(ff.IsStyleAvailable(FontStyle.Regular))
{
regular=true;
fn=new Font(ff,18,FontStyle.Regular);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(ff.IsStyleAvailable(FontStyle.Bold))
{
bold=true;
fn=new Font(ff,18,FontStyle.Bold);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(ff.IsStyleAvailable(FontStyle.Italic))
{
italic=true;
fn=new Font(ff,18,FontStyle.Italic);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(bold && italic)
{
fn=new Font(ff,18,FontStyle.Bold | FontStyle.Italic);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
fn=new Font(ff,18,FontStyle.Underline);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
fn=new Font(ff,18,FontStyle.Strikeout);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
}

b.Dispose();
}

Figure 2 shows the application in action.

Figure 2. The embedded Alpha Dance font.

See the Form1_Paint event handler, it shows specifically how to set the System.Drawing.Font type. It is based on using the System.Drawing.Text.PrivateFontCollection class.

Hope this helps.

Looking for the optimal way to use custom font definitions in WinForms application

Simplest way is to bind the font property in the Properties Window to an application setting, which will end up in the app.config file for your application.



Related Topics



Leave a reply



Submit