Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

FontAwesome image/icon generator for WinForms

0.00/5 (No votes)
14 Feb 2018 1  
Usage of my FontAwesome Image/Icon generator for WinForms

Introduction

First: many thanks for the font to Font Awesome by Dave Gandy - http://fontawesome.io

This is a simple class to create images and icons from FontAwesome TTF.

Background

There are some articles describing usage of FontAwesome in WPF. Here is something for good old WinForms.

(Optional) Preset the class with default properties.

//Sample default properties
FontAwesome.DefaultProperties.Size = 32;
FontAwesome.DefaultProperties.ShowBorder = true;

Now, we can start creating images and icons:

//create image using default properties
var image1 = FontAwesome.Type.Crosshairs.AsImage();

//create icon using default properties
var icon = FontAwesome.Type.Crosshairs.AsIcon();

//create image with custom properties
var image2 = new FontAwesome.Properties(FontAwesome.Type.Square) 
{ ForeColor = Color.White }.AsImage();

Sometimes, something more is needed, so I added some simple support for stacking images together:

//sample of stacking images together to create colored unique icons
var stackedImage = 
    new FontAwesome.Properties(FontAwesome.Type.Square) { ForeColor = Color.White }.AsImage()
        .StackWith(new FontAwesome.Properties(FontAwesome.Type.FileO) 
         { Size = 20, Location = new Point(5, 5), ShowBorder = false })
        .StackWith(new FontAwesome.Properties(FontAwesome.Type.Close) 
         { ForeColor = Color.Red, Size = 14, Location = new Point(13, 13), ShowBorder = false });

The FontAwesome class will even download the TTF file:

FontAwesome.Initialize(); //(optional) initiates TTF check and download

You can disable this behaviour by setting:

FontAwesome.SetDownloadLink(null);

To use the class with C# 2.0, simply remove the FontAwesomeExtensions class.

//C# 2.0 usage sample
var image1 = FontAwesome.Instance.GetImage(FontAwesome.Type.Save);
var image2 = FontAwesome.Instance.GetImage(new FontAwesome.Properties(FontAwesome.Type.TimesCircle) 
             { ForeColor = Color.Red });
var icon = FontAwesome.Instance.GetIcon(new FontAwesome.Properties(FontAwesome.Type.Home) 
           { ForeColor = Color.Blue, BorderColor = Color.Blue, BackColor = Color.White });

Note: GetIcon() generates a GDI object, which is not automatically released. This can cause exceptions if heavily used. See MSDN article for details.

Nuget

You can use Nuget to add the library to your project:

PM> Install-Package Fkosoft.FontAwesome

For the new FontAwesome5, there is a new nuget package:

PM> Install-Package Fkosoft.FontAwesome5

Points of Interest

  • C# 4.0, WinForms

History

  • 1.0.0 - First version
  • 1.0.1 - FontAwesome5 nuget added

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here