2 Attachment(s)
Embedding custom fonts into your application
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)
Attachment 126331
Make sure it is embedded into your application upon build
Attachment 126333
You can copy the following module in your application.
Code:
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
Code:
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
Re: Embedding custom fonts into your application
When Testing This Code I Get An Exception Error Unable To Load Font
Any Suggestions
Re: Embedding custom fonts into your application
Quote:
Originally Posted by
David William Smith
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_
Re: Embedding custom fonts into your application
Quote:
Originally Posted by
ident
The Web Page Was Very Helpful, I Wasn't Using The Applications Namespace In The File String.
Thanks.
Re: Embedding custom fonts into your application
For me it was a missed element
"Set the ‘UseCompatibleTextRendering property ‘, to true (the default is false)"