-
May 29th, 2020, 06:39 PM
#1
Thread Starter
Junior Member
[RESOLVED] Embedded Font
I am trying to use an embedded font throughout my windows form app, and it only seems to work if I have the font installed on my PC. I am using the following code. Any help would be appreciated.
Code:
Imports System.IO
Imports System.Drawing.Text
Imports System.Runtime.InteropServices
Imports System.Reflection
Module CustomFont
'PRIVATE FONT COLLECTION TO HOLD THE DYNAMIC FONT
Private _pfc As PrivateFontCollection = Nothing
Public ReadOnly Property GetInstance(ByVal Size As Single, ByVal style As FontStyle) As Font
Get
'IF THIS IS THE FIRST TIME GETTING AN INSTANCE
'LOAD THE FONT FROM RESOURCES
If _pfc Is Nothing Then LoadFont()
'RETURN A NEW FONT OBJECT BASED ON THE SIZE AND STYLE PASSED IN
Return New Font(_pfc.Families(0), Size, style)
End Get
End Property
Private Sub LoadFont()
Try
'INIT THE FONT COLLECTION
_pfc = New PrivateFontCollection
'LOAD MEMORY POINTER FOR FONT RESOURCE
Dim fontMemPointer As IntPtr = Marshal.AllocCoTaskMem(My.Resources.DUNGEON.Length)
'COPY THE DATA TO THE MEMORY LOCATION
Marshal.Copy(My.Resources.DUNGEON, 0, fontMemPointer, My.Resources.DUNGEON.Length)
'LOAD THE MEMORY FONT INTO THE PRIVATE FONT COLLECTION
_pfc.AddMemoryFont(fontMemPointer,
My.Resources.DUNGEON.Length)
'FREE UNSAFE MEMORY
Marshal.FreeCoTaskMem(fontMemPointer)
Catch ex As Exception
'ERROR LOADING FONT. HANDLE EXCEPTION HERE
End Try
End Sub
End Module
-
May 29th, 2020, 07:23 PM
#2
Re: Embedded Font
Hi Velcrobelly,
I've used this in the past when I had a similar problem.
Load a custom font into your resources, be careful to copy your font name exactly, I seem to remember it's case conscious, and try to avoid spaces in the name.
Code:
'Private font collection.
Private fnt As PrivateFontCollection = Nothing
Public ReadOnly Property GetInstance(ByVal Size As Single, ByVal style As FontStyle) As Font
Get
'If this is the 1st instance, load in the font.
If fnt Is Nothing Then LoadFont()
'Return a new font Object: Style and Size
Return New Font( fnt.Families(0), Size, style)
End Get
End Property
Private Sub LoadFont()
Try
'Initialise the collection.
fnt = New PrivateFontCollection
Dim fontPointer As IntPtr = Marshal.AllocCoTaskMem( _
My.Resources.Your Font Name.Length)
Marshal.Copy(My.Resources.Your Font Name, 0, fontPointer, _
My.Resources.Your Font Name.Length)
fnt.AddMemoryFont(fontPointer, _
My.Resources.Your Font Name.Length)
'Free up memory
Marshal.FreeCoTaskMem(fontPointer)
Catch ex As Exception
'Handle exceptions here
End Try
End Sub
End Module
In the Form just make a single call to the module's GetInstance method to return the font.
Code:
Private Sub LoadFont
Label1.Font = CustomFont.GetInstance(14, FontStyle.Bold)
End Sub
NOTE: On the control that you want to use this font you will need to set the UseCompatibleTextRendering property from False to True.
Poppa
Last edited by Poppa Mintin; May 30th, 2020 at 04:43 AM.
Reason: Typo
Along with the sunshine there has to be a little rain sometime.
-
May 30th, 2020, 02:15 PM
#3
Thread Starter
Junior Member
Re: Embedded Font
Unfortunately, I was unable to make this work.
However, you gave me a clue that allowed my first set of code work to work:
"... you will need to set the UseCompatibleTextRendering property from False to True."
Last edited by Velcrobelly; May 30th, 2020 at 02:52 PM.
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
|