Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 137,227 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,020 people online right now. Registration is fast and FREE... Join Now!





Embed a font into an application

This is a snippet for embedding a font into an application, which ensures if the user doesn't have the font file installed, it really doesn't matter

Submitted By: PsychoCoder
Actions:
Rating:
Views: 590

Language: VB.NET

Last Modified: January 1, 2008
Instructions: First, add the font file as a Resource, that way the font file follows the application. Second you need to add a reference to System.Drawing to gain access to the different font functionalities.

This particular snippet doesn't currently use this a as a Resource, but will be modified, improved to show how to parse a Resource File.

Snippet


  1. 'Namespace Reference
  2. Imports System
  3. Imports System.Drawing
  4.  
  5. 'Global variables
  6. Private Shared PFC As Drawing.Text.PrivateFontCollection
  7. Private Shared NewFont_FF As Drawing.FontFamily
  8.  
  9.  
  10. ''' <summary>
  11. ''' Function to return a new font based on the font file passed to it
  12. ''' </summary>
  13. ''' <param name="name">Path to the new font file</param>
  14. ''' <param name="style">The FontStyle of the new font</param>
  15. ''' <param name="size">T size of the new font</param>
  16. ''' <returns>A new font</returns>
  17. ''' <remarks></remarks>
  18. Private Function CreateFont(ByVal name As String, ByVal style As Drawing.FontStyle, ByVal size As Single, ByVal unit As Drawing.GraphicsUnit) As Drawing.Font
  19.     'Create a new font collection
  20.     PFC = New Drawing.Text.PrivateFontCollection
  21.     'Add the font file to the new font
  22.     '"name" is the qualified path to your font file
  23.     PFC.AddFontFile(name)
  24.     'Retrieve your new font
  25.     NewFont_FF = PFC.Families(0)
  26.  
  27.     Return New Drawing.Font(NewFont_FF, size, style, unit)
  28. End Function
  29.  
  30. 'Example Usage
  31. Label1.Font = CreateFont("pathtofontfile",12,FontStyle.Regular,GraphicsUnit.Point)

Copy & Paste


Comments


demac43 2008-01-06 13:45:23

I get an error message on the line - ..... = PFC.Families(0) System.ArgumentException was unhandled Message="Value of '0' is not valid for 'emSize'. 'emSize' should be greater than 0 and less than or equal to System.Single.MaxValue. Parameter name: emSize" ParamName="emSize" Source="System.Drawing" etc. I am looking to embed "Colonna.ttf" to use in a label. It has been added to resources Demac43 (novice class)

PsychoCoder 2008-01-06 16:22:49

Show me how you are calling it, I used this in an application before. Lets see if we can debug this for you :)

demac43 2008-01-07 01:38:40

Imports System Imports System.Drawing Public Class Form1 Dim ...... Dim ...... Private Shared PFC As Drawing.Text.PrivateFontCollection Private Shared NewFont_FF As FontFamily Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.MaximizeBox = True ..... .....

demac43 2008-01-07 01:50:17

the error occurs at build time Function is written inside the class module Imports System Imports System.Drawing Public Class Form1 Dim ...... Dim ...... Private Shared PFC As Drawing.Text.PrivateFontCollection Private Shared NewFont_FF As FontFamily Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.MaximizeBox = True ..... ..... lblTheHeader.Font = CreateFont("Colonna.ttf", 12, FontStyle.Regular, GraphicsUnit.Point) Private Function CreateFont(ByVal name As String, ByVal style As Drawing.FontStyle, ByVal size As Single, ByVal unit As Drawing.GraphicsUnit) As Drawing.Font PFC = New Drawing.Text.PrivateFontCollection PFC.AddFontFile(name) NewFont_FF = PFC.Families(0) Return New Drawing.Font(NewFont_FF, size, style, unit) End Function End Class regards demac43 ...

RodgerB 2008-01-20 06:36:54

@demac43: You have the size parameter mixed up with the style parameter. @PsychoCoder: Could you please explain how I can use this with a resource? Also, I have found if you make more than one request to use this function, as in calling it twice, it provokes an exception. I wanted to use the same font, just a different size! :P Thanks :).


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month