# VBForums CodeBank > CodeBank - Visual Basic .NET >  Embedding custom fonts into your application

## TheBarret

Theire are several ways to make use of a font type that is not standard on a platform your application is executed from, i tried most of them and ive come to learn that the following method was best for my desires.


First we add and embed our ttf (font) file in the application 
(in this case im using the Fixedsys Excelsior 3.01)



Make sure it is embedded into your application upon build




You can copy the following module in your application.



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

Module Helpers
    Public Function LoadFont(Asm As Assembly, Name As String, Size As Integer, Style As FontStyle) As Font
        Using Collection As New PrivateFontCollection
            Dim Bytes() As Byte = Helpers.FontData(Asm, Name)
            Dim Ptr As IntPtr = Marshal.AllocCoTaskMem(Bytes.Length)
            Marshal.Copy(Bytes, 0, Ptr, Bytes.Length)
            Collection.AddMemoryFont(Ptr, Bytes.Length)
            Marshal.FreeCoTaskMem(Ptr)
            Return New Font(Collection.Families(0), Size, Style)
        End Using
    End Function
    Private Function FontData(Asm As Assembly, Name As String) As Byte()
        Using Stream As Stream = Asm.GetManifestResourceStream(Name)
            If (Stream Is Nothing) Then Throw New Exception(String.Format("Unable to load font '{0}'", Name))
            Dim Buffer() As Byte = New Byte(CInt(Stream.Length - 1)) {}
            Stream.Read(Buffer, 0, CInt(Stream.Length))
            Return Buffer
        End Using
    End Function
End Module
```


To initialize the font into a control for let say textbox or richttextbox, use the following statement



```
MyControl.Font = Helpers.LoadFont(Me.GetType.Assembly, "Sirc.FSEX300.ttf", 9, FontStyle.Regular)
```

Notice that it does require the application namespace to locate the font data in the current assembly, 
without it, it will not work.


I hope it serves you well as it did for me.

- Barret

----------


## David William Smith

When Testing This Code I Get An Exception Error Unable To Load Font

Any Suggestions

----------


## ident

> When Testing This Code I Get An Exception Error Unable To Load Font
> 
> Any Suggestions



we cant pull a magic wand out. Post your code. Not mind readers.

https://docs.microsoft.com/en-us/dot...System_String_

----------


## David William Smith

> we cant pull a magic wand out. Post your code. Not mind readers.
> 
> https://docs.microsoft.com/en-us/dot...System_String_


The Web Page Was Very Helpful, I Wasn't Using The Applications Namespace In The File String.

Thanks.

----------


## 6h057

For me it was a missed element

"Set the UseCompatibleTextRendering property , to true (the default is false)"

----------

