-
May 3rd, 2015, 08:15 AM
#1
Thread Starter
Member
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)
Make sure it is embedded into your application upon build
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
-
Jul 12th, 2020, 12:51 PM
#2
New Member
Re: Embedding custom fonts into your application
When Testing This Code I Get An Exception Error Unable To Load Font
Any Suggestions
-
Jul 13th, 2020, 07:12 PM
#3
Re: Embedding custom fonts into your application
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_
Last edited by ident; Jul 13th, 2020 at 07:15 PM.
-
Jul 14th, 2020, 08:02 AM
#4
New Member
Re: Embedding custom fonts into your application
Originally Posted by ident
The Web Page Was Very Helpful, I Wasn't Using The Applications Namespace In The File String.
Thanks.
-
Jan 10th, 2022, 02:48 PM
#5
Member
Re: Embedding custom fonts into your application
For me it was a missed element
"Set the ‘UseCompatibleTextRendering property ‘, to true (the default is false)"
Last edited by 6h057; Jan 12th, 2022 at 01:43 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|