Click here to Skip to main content
15,867,977 members
Articles / Programming Languages / C# 4.0
Tip/Trick

FontAwesome image/icon generator for WinForms

Rate me:
Please Sign up or sign in to vote.
4.92/5 (24 votes)
14 Feb 2018CPOL 56.1K   2.7K   35   19
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.

Image 1

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.

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

Now, we can start creating images and icons:

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

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

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

You can disable this behaviour by setting:

C#
FontAwesome.SetDownloadLink(null);

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

C#
//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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Slovakia Slovakia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAwesome! Pin
Gopinath K.P26-Apr-20 22:43
Gopinath K.P26-Apr-20 22:43 
QuestionFontAwsome 5 - Initialisation Method missing Pin
David James Tuke25-Jul-18 19:27
professionalDavid James Tuke25-Jul-18 19:27 
AnswerRe: FontAwsome 5 - Initialisation Method missing Pin
Frantisek Ruzicka1-Oct-18 23:26
professionalFrantisek Ruzicka1-Oct-18 23:26 
GeneralMy vote of 5 Pin
Franc Morales18-Jun-18 21:41
Franc Morales18-Jun-18 21:41 
QuestionGithub Pin
SpArtA17-Feb-18 0:21
SpArtA17-Feb-18 0:21 
AnswerRe: Github Pin
Frantisek Ruzicka19-Feb-18 12:47
professionalFrantisek Ruzicka19-Feb-18 12:47 
GeneralMy vote of 5 Pin
Member 1339763814-Feb-18 5:02
Member 1339763814-Feb-18 5:02 
Questionunless you are the creator of the FontAwesome font Pin
BillWoodruff13-Feb-18 4:43
professionalBillWoodruff13-Feb-18 4:43 
AnswerRe: unless you are the creator of the FontAwesome font Pin
Frantisek Ruzicka14-Feb-18 6:21
professionalFrantisek Ruzicka14-Feb-18 6:21 
QuestionMy vote of 4 ... Pin
DSAlCoda16-Nov-16 7:03
DSAlCoda16-Nov-16 7:03 
... is just because I'm a VB guy and I have a feeling some of what you have in your code may not translate easily. But, maybe it will since I'm more a VB guy and don't really understand the C# code.

But, other than that, I really like FontAwesome and try to incorporate it (and Bootstrap) wherever possible. Having those same FA items available in WinForms would be great.
AnswerRe: My vote of 4 ... Pin
Frantisek Ruzicka16-Nov-16 9:22
professionalFrantisek Ruzicka16-Nov-16 9:22 
GeneralRe: My vote of 4 ... Pin
DSAlCoda17-Nov-16 8:59
DSAlCoda17-Nov-16 8:59 
GeneralRe: My vote of 4 ... Pin
Frantisek Ruzicka17-Nov-16 10:12
professionalFrantisek Ruzicka17-Nov-16 10:12 
General... and ...... 5 Pin
DSAlCoda17-Nov-16 10:23
DSAlCoda17-Nov-16 10:23 
GeneralRe: ... and ...... 5 Pin
Frantisek Ruzicka17-Nov-16 10:39
professionalFrantisek Ruzicka17-Nov-16 10:39 
GeneralMy vote of 5 Pin
Farhad Reza15-Nov-16 1:17
Farhad Reza15-Nov-16 1:17 
GeneralRe: My vote of 5 Pin
Frantisek Ruzicka15-Nov-16 2:07
professionalFrantisek Ruzicka15-Nov-16 2:07 
GeneralMy vote of 5 Pin
wmjordan11-Nov-16 14:42
professionalwmjordan11-Nov-16 14:42 
GeneralRe: My vote of 5 Pin
Frantisek Ruzicka12-Nov-16 10:58
professionalFrantisek Ruzicka12-Nov-16 10:58 

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.