Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Import Custom Font's via Ressources

[Import Custom Font's via Ressources]

This can come in handy when you want to use a custom font,

but your app needs to run without admin rights (because of ClickOnce or other least privileged requirements).

Without admin rights you cannot install fonts on a system.

With this tutorial allows you too import a font file straight from your applications ressources without any worries of user's needing the fonts installed!


1. Create Module


The Module Can be Named Whatever You Want it to be Named!

Module Created by :: MATTHEW KLEINWAKS
[img]http://www.winsocketdotnetworkprogramming.com/xmlwebservicesaspnetworkprogramming11chap_files/xmlwebservicesasprogramming215.png[/img]



2. Open and Replace the Module With the Following Code


Replace "DIGITALDREAMNARROW" With the Name of The Font Will be Using. ex: Open_Sans

Make Sure it's The Same Name (Case Sensitive) as The File You Will Be Creating in Step #3

Code
Imports System.Drawing.Text
Imports System.Runtime.InteropServices

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.DIGITALDREAMNARROW.Length)

            'COPY THE DATA TO THE MEMORY LOCATION
            Marshal.Copy(My.Resources.DIGITALDREAMNARROW, _
                         0, fontMemPointer, _
                         My.Resources.DIGITALDREAMNARROW.Length)

            'LOAD THE MEMORY FONT INTO THE PRIVATE FONT COLLECTION
            _pfc.AddMemoryFont(fontMemPointer, _
                               My.Resources.DIGITALDREAMNARROW.Length)

            'FREE UNSAFE MEMORY
            Marshal.FreeCoTaskMem(fontMemPointer)
        Catch ex As Exception
            'ERROR LOADING FONT. HANDLE EXCEPTION HERE
        End Try

    End Sub

End Module



3. Adding the Font File to Your Ressources, ex: Open_Sans


Make sure once again that it is the same name as in Step #2 (Case Sensitive)

I know there are multiple ways of adding resources, I prefer this method!

[img]http://static.daniweb.com/images/attachments/3/img1.jpg[/img]
[img]http://pad1.whstatic.com/images/thumb/8/83/Save-File-from-Your-Resources-in-Vb.Net-Step-6.jpg/550px-Save-File-from-Your-Resources-in-Vb.Net-Step-6.jpg[/img]



4. Change Font's on Your Labels!



NameOfClass.GetInstance(FONT_SIZE, FONT_TYPE)

Code
Label1.Font = CustomFont.GetInstance(12, FontStyle.Bold)

[Image: wlQx51C.png]
from file
Dim pfc As New Drawing.Text.PrivateFontCollection()
pfc.AddFontFile("C:\Font.ttf")
Font = New Font(pfc.Families(0), 10, FontStyle.Regular)
(Sep 29, 2013 09:21 PM)Only3 Wrote:  from file
Dim pfc As New Drawing.Text.PrivateFontCollection()
pfc.AddFontFile("C:\Font.ttf")
Font = New Font(pfc.Families(0), 10, FontStyle.Regular)

Yes but this way you need your separate file while this way it all-in-one. No need for third party directory!
[Image: wlQx51C.png]
try this
Dim res = My.Resources.font
Dim pfc As New Drawing.Text.PrivateFontCollection
pfc.AddMemoryFont(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(res, 0), res.Length)
Font = New Font(pfc.Families(0), 10, FontStyle.Regular)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by john ronaldsadJul 20, 2022 06:49 PM 4 238 Jul 20, 2022 06:49 PM
Last Post: john ronaldsad
[Shell] by Emre_May 27, 2020 04:36 AM 0 105 May 27, 2020 04:36 AM
Last Post: Emre_
[Python] by Johan CruyffApr 4, 2020 08:15 AM 2 126 Apr 4, 2020 08:15 AM
Last Post: lnvisible