Embedding Fonts in Visual Basic Apps

Monday Feb 6th 2017 by Hannes DuPreez
Share:

Here's a solution for times when you need to embed a font into your application.

Having a well thought-out design, coupled with visual appeal, is crucial for modern day apps. In some situations, there may be a need to make use of custom fonts for your applications. In cases like these, it is easy to embed the custom font in your application and make use of it. You have to keep in mind that some fonts may not exist on other people's PC's, and especially when your company has designed a custom font, you still need the same look and feel you have intended. If the desired system does not find the font in question, it defaults to a font that may look similar.

Let's have a closer look.

Fonts

For the uninformed: A font is the style of text that you use to type with—basically your digital handwriting—but at least you have more options and aren't stuck with your real handwriting—nobody would have understood any of my articles, because my handwriting is extremely atrocious and barely legible.… A font is comprised of a typeface, font style, and a font size.

Types of Fonts

Font Tools

There are several tools available to create your own fonts. The following can help you find some pretty decent free font editors or paid font editors:

Our Project

The aim of our little project is to embed a font into our program and use it productively, even distribute it along with your program.

Create a new Visual Basic Windows Forms project. Once the form has loaded, add one Textbox. This Textbox will be used to demonstrate how to use the font as an embedded resource.

To add a Font Resource to your program, use the following steps:

  1. Click Project.
  2. Click PROJECTNAME Properties.
  3. Select Resources.
  4. Select Files.
  5. Select Add Resource.
  6. Browse to your Font file.

These steps are demonstrated in Figure 1.

Adding a Resource
Figure 1: Adding a Resource

Make sure the font is embedded in your program by following the next steps:

  1. Select the Font file in your Solution Explorer.
  2. Open the Properties Window.
  3. Set the 'Build Action' property to Embed, as shown in Figure 2.

    Embed the font
    Figure 2: Embed the font

Add a new Module to your project and add the following Namespaces to it:

Imports System.IO
Imports System.Reflection
Imports System.Drawing.Text
Imports System.Runtime.InteropServices

Add the following Function:

   Public Function GetFont(aAssembly As Assembly, _
      strFontName As String, intFontSize As Integer, _
      fsFontStyle As FontStyle) As Font

      Using pcolFonts As New PrivateFontCollection

         Dim bFont() As Byte = modEmbedFont. _
            bRawFontData(aAssembly, strFontName)
         Dim ptrMemFont As IntPtr = _
            Marshal.AllocCoTaskMem(bFont.Length)

         Marshal.Copy(bFont, 0, ptrMemFont, bFont.Length)
         pcolFonts.AddMemoryFont(ptrMemFont, bFont.Length)

         Marshal.FreeCoTaskMem(ptrMemFont)

         Return New Font(pcolFonts.Families(0), _
            intFontSize, fsFontStyle)

      End Using

   End Function8.

Here, I have created a PrivateFontCollection object to store our font that we'd like to use. I then make use of a Marshal object to copy the contents of the font into memory with the help of the bRawFontData function. This function will be added now.

Add the following function:

   Private Function bRawFontData(aAssembly As Assembly, _
      strFontName As String) As Byte()

     Using stFont As Stream = _
         aAssembly.GetManifestResourceStream(strFontName)

         If (stFont Is Nothing) Then Throw _
            New Exception(String.Format("Cannot load _
            font '{0}'", strFontName))

         Dim bFontBuffer() As Byte = New _
            Byte(CInt(stFont.Length - 1)) {}

         stFont.Read(bFontBuffer, 0, CInt(stFont.Length))

         Return bFontBuffer

      End Using

   End Function

The preceding code reads the font via the use of a Stream object. As it reads each byte, it feeds the previous function named GetFont. Navigate to the Form and add the following code:

   Private Sub Form1_Load(sender As Object, e As EventArgs) _
      Handles MyBase.Load

      TextBox1.Font = modEmbedFont.GetFont(Me.GetType.Assembly, _
         "Embed_Font_Ex.MISTRAL.TTF", 12, FontStyle.Bold)

   End Sub

The Textbox's font property gets set to the object that gets returned from the GetFont Function. Figure 3 shows the result in which the Textbox's Font is changed to the embedded font.

The TextBox Font is changed to the Embedded Font
Figure 3: The TextBox Font is changed to the Embedded Font

Conclusion

It is easy to embed a font into your application so that you do not have to worry whether or not the user will have the desired font to make your app's look and feel as you intended it to be.

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved