CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Embedded fonts

  1. #1
    Join Date
    Jul 2004
    Posts
    42

    Embedded fonts

    I havent had much luck in getting sound advice from embedding but heres another stab at it. I use the code from VB.NET F.A.Q for telling me how to embedd and use fonts so I don't have to install them on users computers that have my programs. Everything is the same that I have used but it don't work for me unless I install the font on my computer and this defeats the purpose of embedding it since I can't share it.

    The only code I use diffrent is the part about accessing the resource:
    Code:
    Me.Font = New Font(FntFC.Families(0), 10)
    I have tried to replace it with this: By the way... the name of my embedded font is DotMatrix and it's a true type font.
    Code:
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            Dim g As Graphics = e.Graphics
    'Text to be drawn to the form
            Dim txt As String = "Hello"
            g.DrawString(txt, New Font("FntFC.DotMatrix", 18, FontStyle.Regular), Brushes.Black, New RectangleF(20, 20, 180, 30))
    End Sub

  2. #2
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Re: Embedded fonts

    Assuming you followed the directions on that sample, here is how you would use it:
    Code:
                Dim sMyFonts As String() = {"DotMatrix"}
                Dim fEmbedded As New Font(GetFont(sMyFonts).Families(0), 10)
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  3. #3
    Join Date
    Jul 2004
    Posts
    42

    Re: Embedded fonts

    Craig,

    I have been messing around with this for a while now and still haven't got it to do anything. Here's what I have done so far that hasn't worked. Is it something i'm doing or ?? keep in mind that I use vb.net 2002

    First, I properly embedded the TTF file name Dotmatrx.ttf as an embedded resource. I verified that when the font is registered it shows in the system as DotMatrix

    My code reads:
    Code:
    Imports System
    Imports System.IO
    Public Class Form1
    
        Inherits System.Windows.Forms.Form
    
    + Windows Form Designer Generated Code
    
    Public Function GetFont(ByVal FontResource() As String) As _
        Drawing.Text.PrivateFontCollection
            'Get the namespace of the application    
            Dim NameSpc As String = _
                Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()
            Dim FntStrm As IO.Stream
            Dim FntFC As New Drawing.Text.PrivateFontCollection()
            Dim i As Integer
            For i = 0 To FontResource.GetUpperBound(0)
                'Get the resource stream area where the font is located
                FntStrm = _
            Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( _
            NameSpc + "." + FontResource(i))
                'Load the font off the stream into a byte array 
                Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte
                FntStrm.Read(ByteStrm, 0, Int(CType(FntStrm.Length, Integer)))
                'Allocate some memory on the global heap
                Dim FntPtr As IntPtr = _
                    Runtime.InteropServices.Marshal.AllocHGlobal( _
                    Runtime.InteropServices.Marshal.SizeOf(GetType(Byte)) * _
                        ByteStrm.Length)
                'Copy the byte array holding the font into the allocated memory.
                Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, _
                    FntPtr, ByteStrm.Length)
                'Add the font to the PrivateFontCollection
                FntFC.AddMemoryFont(FntPtr, ByteStrm.Length)
                'Free the memory
                Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr)
            Next
            Return FntFC
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    Dim sMyFonts As String() = {"DotMatrix"}
                Dim fEmbedded As New Font(GetFont(sMyFonts).Families(0), 10)
    Label1.Font = fEmbedded
    End Sub

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

Featured