Solved

VB.NET 2010 Embed Font

Posted on 2010-09-17
11
1,023 Views
Last Modified: 2013-11-26
I'm trying to figure out how to embed a font into an application.  I don't want the font to be installed on the user's machine, but rather simply be used in a single control on the form.  I've tried numerous "embed font" code snippets only to run into errors with each one.

Any help appreciated.
0
Comment
Question by:Javin007
  • 7
  • 3
11 Comments
 
LVL 6

Expert Comment

by:ajb2222
ID: 33702796
0
 
LVL 4

Author Comment

by:Javin007
ID: 33703021
That is precisely one of the code snippets I checked out that did nothing but produce errors.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33703030
0
 
LVL 4

Author Comment

by:Javin007
ID: 33703341
I saw it, but I did not try it for the following reason:


My question:

"I don't want the font to be installed on the user's machine..."


That demo:

"The steps we will do here for the demonstration are:
"Install the font
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33703380
you need to install it on your development PC but not on other PCs
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Author Comment

by:Javin007
ID: 33703630
This project is in C#.  I don't have any C# IDEs installed on my system.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33703700
you can take FontEmbedding.dll from my zip file!
0
 
LVL 4

Accepted Solution

by:
Javin007 earned 0 total points
ID: 33703962
I came up with a method on my own:

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

Public Class FontResource
    Private PFC As PrivateFontCollection = Nothing

    Public Sub New(ByVal Res() As Byte)
        LoadFont(Res)
    End Sub

    Public ReadOnly Property GetInstance(ByVal Size As Single, ByVal style As FontStyle) As Font
        Get
            If PFC Is Nothing Then Return Nothing
            Return New Font(PFC.Families(0), Size, style)
        End Get
    End Property

    Public Function LoadFont(ByVal Res() As Byte) As Boolean
        Try
            PFC = New PrivateFontCollection
            Dim fontMemPointer As IntPtr = Marshal.AllocCoTaskMem(Res.Length)
            Marshal.Copy(My.Resources.VeraMono, 0, fontMemPointer, Res.Length)
            PFC.AddMemoryFont(fontMemPointer, Res.Length)
            Marshal.FreeCoTaskMem(fontMemPointer)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

End Class


Then call the resource by:

        Dim fnt As New FontResource(My.Resources.VeraMono)
        txtSQL.Font = fnt.GetInstance(8, FontStyle.Regular)


If anyone sees a problem with this method, please let me know.
0
 
LVL 4

Author Comment

by:Javin007
ID: 33704398
Correction... While this method throws no errors, and does change the size of the text, it does not actually use the embedded font.  >:(
0
 
LVL 4

Author Comment

by:Javin007
ID: 33704456
Grr.  As it turns out, Textboxes have no "UseCompatibleTextRendering" property.  Still looking for suggestions.
0
 
LVL 4

Author Closing Comment

by:Javin007
ID: 34078567
No valid answers given.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now