Results 1 to 6 of 6

Thread: Using a Font not installed on the Computer

Threaded View

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Using a Font not installed on the Computer

    Hi!!

    It may have happen to you a time where u needed to use a font that is not (or normally is not) installed in the target computer. You can install it, but installing fonts slows down the system and may need restart. So I went looking (well kleinma may have helped a little) and found this link http://www.bobpowell.net/embedfonts.htm. But as this codes deals with unmanaged memory (and some times can occur some errors), I changed the code to create a temporary file. In my code I used a very uncommon font TI83Pluspc, is the font used by the Graphic Calculator TI-83 Plus, so where is says "\TI-83PL.TTF" you should change by the name of your font file.

    First you need to add the font file to the project as you would an image, a sound, etc. Then use this code:
    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Dim FontCollection As New System.Drawing.Text.PrivateFontCollection()
    3.     'Filestream that will create the temporary font file, uses IO.Path.GetTempPath so the file is created in a Temporary folder
    4.     Dim FileStream As New IO.FileStream(IO.Path.GetTempPath & "\TI-83PL.TTF", IO.FileMode.Create, IO.FileAccess.Write)
    5.     'Creates the File, you can use My.Resources.TI_83PL because its a byte array
    6.     FileStream.Write(My.Resources.TI_83PL, 0, My.Resources.TI_83PL.Length)
    7.     FileStream.Close()
    8.     'Adds the Font to the font collection
    9.     FontCollection.AddFontFile(IO.Path.GetTempPath & "\TI-83PL.TTF")
    10.  
    11.     Dim f As System.Drawing.Font
    12.     'We will need the FontFamily because of the font styles, because some fonts dont include all styles
    13.     Dim ff As FontFamily
    14.     'Loop through the Families
    15.     For Each ff In FontCollection.Families
    16.         'If the regular style is available then uses it, you can use some other style just change this "FontStyle.Regular"
    17.         'For the style you want. You can also change its size
    18.         If ff.IsStyleAvailable(FontStyle.Regular) Then f = New Font(ff, 8.25, FontStyle.Regular, GraphicsUnit.Point)
    19.     Next
    20.     'Assign the font to the controls you want to use it in
    21.     Textbox1.Font = f
    22. End Sub
    And to keep the temporary file from staying on the computer, occupying space you should use this:
    vb Code:
    1. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    2.     'As IO.Path.GetTempPath is readonly you don't need to have the path assign to a variable, you can just use this
    3.     IO.File.Delete(IO.Path.GetTempPath & "\TI-83PL.TTF")
    4. End Sub

    I'm not sure if it will work with all Font types. But with TrueType works perfectly.
    Last edited by Lasering; Nov 13th, 2007 at 06:14 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width