Private Font Collection - writing with private fonts

Private Font Collection - writing with private fonts

Post by Ken Tucker » Wed, 12 Oct 2005 09:35:25


Hi,

This works. I never got around to installing this font on my
laptop.

Dim pc As New System.Drawing.Text.PrivateFontCollection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Try
pc.AddFontFile("c:\barcoding.ttf")
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim fnt As Font = New Font(pc.Families(0), 30, FontStyle.Regular)
e.Graphics.DrawString("Test", fnt, Brushes.Red, 10, 10)
End Sub

Link to font.
http://www.yqcomputer.com/

Ken
-----------------------
 
 
 

Private Font Collection - writing with private fonts

Post by Bob Powell » Wed, 12 Oct 2005 15:35:51

In addition to Ken's suggestion you might want to read the article on
embedding true type fonts in Windows Forms Tips and Tricks.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.yqcomputer.com/

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.yqcomputer.com/

Answer those GDI+ questions with the GDI+ FAQ
http://www.yqcomputer.com/

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

 
 
 

Private Font Collection - writing with private fonts

Post by Qm9iQWNoZ2 » Wed, 12 Oct 2005 21:29:06

Hi Bob!

I had tried your code and for some reason got this error
An unhandled exeption of type ystem.NullReferenceExceptionoccurred in
EmbedTTF.exe
Additional information: Object reference not set to an instance of an object.


I got the error on the Dim data line (see below) but I think the problem lay
with the previous line not picking up the Radio font properly. I think this
because I watched debug autos and it looked like the
mbedTTF.vb.Radio___.TTFdid not work since the fontStream was othing
after it left that line.

Yes. I added the reference for Radio___.TTF just like you show in your
example. Am I right in thinking that the culprit is the fontStream not being
loaded properly since if it value is othing?

Any ideas?

'load the resource

Dim fontStream As Stream =
Me.GetType().Assembly.GetManifestResourceStream("EmbedTTF.vb.RADIO___.TTF")

'create an unsafe memory block for the data

Dim data As System.IntPtr = Marshal.AllocCoTaskMem(fontStream.Length)

Bob


"Bob Powell [MVP]" wrote>

> In addition to Ken's suggestion you might want to read the article >n
> embedding true type fonts in Windows Forms Tips and Tric>s. >>
> >-
> Bob Powell [M>P]
> Visual C#, System.Draw>ng >>
> Ramuseco Limited .NET consult>ng
> http://www.yqcomputer.com/ .>om >>
> Find great Windows Forms articles in Windows Forms Tips and Tri>ks
> http://www.yqcomputer.com/ ;tm >>
> Answer those GDI+ questions with the GDI+ >AQ
> http://www.yqcomputer.com/ ;tm >>
> All new articles provide code in C# and VB.N>T.
> Subscribe to the RSS feeds provided and never miss a new artic>e. >> >> >> >> >>
> "BobAchg<ll" < XXXX@XXXXX.COM >.com> wrote in messa>e

> > Has anyone had success with writing text using a private font that was >o>
> > already installed to the syst>m>
> > When I try the example code in NET Framework Developer's Guide cal>e>
> > "Creating a Private Font Collecti>n>
> > ... I only see a default font written to the form NOT the private fo>t>
> > t>a>
> > was loaded. Ofcourse I see the right font written if the font is alrea>y>
> > >n>
> > that is installed in the system. But if the private font that I load w>t>
> > the AddFontFile method is one that has not been already installed in >h>
> > Windows system then the example text is written in a default fo>t>
> > I am guessing this is my problem and not a bug in the Visual Basic .>e>
> > class. Can you set me straight as to what I might be doing wro>g>
> > Than>s>
> > >o>
>>> >> >>
 
 
 

Private Font Collection - writing with private fonts

Post by Qm9iQWNoZ2 » Wed, 12 Oct 2005 22:20:05

Thanks Ken!

You are a life saver.

After looking through your code I can see where the error is in the example
code in NET Framework Developer's Guide called
reating a Private Font Collection
Everywhere they have the code:

Dim regFont As New Font( _
familyName, _
16, _
FontStyle.Regular, _
GraphicsUnit.Pixel)

They need to change the "familyName" to be instead "fontFamilies(j)"

So the code will look like this:

Dim regFont As New Font( _
fontFamilies(j), _
16, _
FontStyle.Regular, _
GraphicsUnit.Pixel)

The guy who wrote the example was mistakenly trying to make the new font by
pointing to the name of the font family. He should have been pointing to the
family itself. What's in a name? Ahh! So profound for so early in the
morning.

Thanks again Ken!


>> Hi, >> >> This works. I never got around to installing this font on my >> laptop. >> >> Dim pc As New System.Drawing.Text.PrivateFontCollection >> >> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As >> System.EventArgs) Handles MyBase.Load >> >> Try >> pc.AddFontFile("c:\barcoding.ttf") >> Catch ex As Exception >> Trace.WriteLine(ex.ToString) >> End Try >> End Sub >> >> Private Sub Form1_Paint(ByVal sender As Object, ByVal e As >> System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint >> Dim fnt As Font = New Font(pc.Families(0), 30, FontStyle.Regular) >> e.Graphics.DrawString("Test", fnt, Brushes.Red, 10, 10) >> End Sub >> >> Link to font. >> http://www.yqcomputer.com/ >> >> Ken >> -----------------------

>>>> >>>> Has anyone had success with writing text using a private font that was not >>>> already installed to the system? >>>> >>>> When I try the example code in NET Framework Developer's Guide called >>>> "Creating a Private Font Collection" >>>> >>>> ... I only see a default font written to the form NOT the private font >>>> that >>>> was loaded. Ofcourse I see the right font written if the font is already >>>> one >>>> that is installed in the system. But if the private font that I load with >>>> the AddFontFile method is one that has not been already installed in the >>>> Windows system then the example text is written in a default font. >>>> >>>> I am guessing this is my problem and not a bug in the Visual Basic .Net >>>> class. Can you set me straight as to what I might be doing wrong? >>>> >>>> Thanks! >>>> >>>> Bob >>>> >> >> >>
 
 
 

Private Font Collection - writing with private fonts

Post by Bob Powell » Wed, 12 Oct 2005 23:49:59

t sounds to me as if you've not set the embedded resource properties
correctly and the stream is empty after the fetch from the resources.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"BobAchgill" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...