Embedding Fonts in Visual Basic Apps | CodeGuru

Embedding Fonts in Visual Basic Apps

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 […]

Written By
Hannes DuPreez
Hannes DuPreez
Feb 6, 2017
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

Adding a Resource

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.

Hannes DuPreez

Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.