Imports:
code:
<br />
Imports System<br />
Imports System.Drawing<br />
First import your .tff file in to the resource
don't forget to change the
Build Action to set on: Embedded Resource
code:
<br />
Private Shared _EmbeddedFonts As Drawing.Text.FontCollection<br />
Private Shared FontArray() As String = {"chp-fire.ttf"}<br />
<br />
Dim FntFC As Drawing.Text.PrivateFontCollection = GetFont(FontArray)<br />
code:
<br />
#Region " Load Font From Recource "<br />
Private Declare Auto Function AddFontMemResourceEx Lib "Gdi32.dll" _<br />
(ByVal pbFont As IntPtr, ByVal cbFont As Integer, _<br />
ByVal pdv As Integer, ByRef pcFonts As Integer) As IntPtr<br />
<br />
Public Function GetFont(ByVal FontResource() As String) As _<br />
Drawing.Text.PrivateFontCollection<br />
'Get the namespace of the application <br />
Dim NameSpc As String = _<br />
Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()<br />
Dim FntStrm As IO.Stream<br />
Dim FntFC As New Drawing.Text.PrivateFontCollection<br />
Dim i As Integer<br />
For i = 0 To FontResource.GetUpperBound(0)<br />
'Get the resource stream area where the font is located<br />
FntStrm = _<br />
Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( _<br />
NameSpc + "." + FontResource(i))<br />
'Load the font off the stream into a byte array<br />
Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte<br />
FntStrm.Read(ByteStrm, 0, Int(CType(FntStrm.Length, Integer)))<br />
'Allocate some memory on the global heap<br />
Dim FntPtr As IntPtr = _<br />
Runtime.InteropServices.Marshal.AllocHGlobal( _<br />
Runtime.InteropServices.Marshal.SizeOf(GetType(Byte)) * _<br />
ByteStrm.Length)<br />
'Copy the byte array holding the font into the allocated memory.<br />
Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, _<br />
FntPtr, ByteStrm.Length)<br />
'Add the font to the PrivateFontCollection<br />
FntFC.AddMemoryFont(FntPtr, ByteStrm.Length)<br />
Dim pcFonts As Int32<br />
pcFonts = 1<br />
AddFontMemResourceEx(FntPtr, ByteStrm.Length, 0, pcFonts)<br />
'Free the memory<br />
Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr)<br />
Next<br />
Return FntFC<br />
End Function<br />
#End Region<br />
To load the font.
if you wanna use a label font
code:
<br />
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br />
Label1.Font = New Font(FntFC.Families(0), 45, FontStyle.Bold, GraphicsUnit.Point)<br />
<br />
End Sub<br />
to set the main font
code:
<br />
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br />
me.Font = New Font(FntFC.Families(0), 45, FontStyle.Bold, GraphicsUnit.Point)<br />
<br />
End Sub<br />