Click to See Complete Forum and Search --> : Embedded fonts
teamdad
September 24th, 2004, 12:02 AM
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 (http://www.tek-tips.com/faqs.cfm?fid=4747) 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:
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.
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
Craig Gemmill
September 25th, 2004, 07:37 PM
Assuming you followed the directions on that sample, here is how you would use it:
Dim sMyFonts As String() = {"DotMatrix"}
Dim fEmbedded As New Font(GetFont(sMyFonts).Families(0), 10)
teamdad
October 4th, 2004, 11:57 PM
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:
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.