By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,190 Members | 2,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,190 IT Pros & Developers. It's quick & easy.

Private Font Collection – writing with private fonts

P: n/a

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

Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.dafont.com/download/?os=win&file=barcoding

Ken
-----------------------
"BobAchgill" <Bo********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...

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

Nov 21 '05 #2

P: n/a
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.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" <Bo********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...

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

Nov 21 '05 #3

P: n/a
Hi Bob!

I had tried your code and for some reason got this error…

An unhandled exeption of type ‘System.NullReferenceException’ occurred 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
“EmbedTTF.vb.Radio___.TTF” did not work since the fontStream was “Nothing”
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’s value is “Nothing”??

Any ideas?

'load the resource

Dim fontStream As Stream =
Me.GetType().Assembly.GetManifestResourceStream("E mbedTTF.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 on
embedding true type fonts in Windows Forms Tips and Tricks.

--
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" <Bo********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...

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


Nov 21 '05 #4

P: n/a
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
“Creating 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!

"Ken Tucker [MVP]" wrote:
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.dafont.com/download/?os=win&file=barcoding

Ken
-----------------------
"BobAchgill" <Bo********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...

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


Nov 21 '05 #5

P: n/a
It 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" <Bo********@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
Hi Bob!

I had tried your code and for some reason got this error.

An unhandled exeption of type 'System.NullReferenceException' occurred 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
"EmbedTTF.vb.Radio___.TTF" did not work since the fontStream was "Nothing"
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's value is "Nothing"??

Any ideas?

'load the resource

Dim fontStream As Stream =
Me.GetType().Assembly.GetManifestResourceStream("E mbedTTF.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 on
embedding true type fonts in Windows Forms Tips and Tricks.

--
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" <Bo********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
>
> 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
>


Nov 21 '05 #6

P: n/a
Hi Bob

I really hope you (or anyone) can shed some light on my problem. I have
written a calculator application in VB.NET for VS2005. It uses an LCD font
to display the result of calculations. I followed your instructions and
created a private font collection with the LCD font as an embedded resource.
On the form load I assign the font to the label control which displays the
calculated results.

I created a deployment project and built an msi for my calculator. I have
installed this calculator on 4 machines. On 2 of them the LCD font displays
as expected (as it does when debugging in VS 2005). On the other 2 it
displays incorrect characters in some undeterminable font.

I have checked the version of .NET on the target machines and there doesnt
seem to be any pattern. I also wondered if it was something to do with the
version of windows installer (setup.exe) but that doesnt seem to be the case
either.

Can you suggest any other avenue of enquiry please ? I feel really stuck
with this one.

Bob

"Bob Powell [MVP]" wrote:
It 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" <Bo********@discussions.microsoft.comwrote in message
news:7F**********************************@microsof t.com...
Hi Bob!

I had tried your code and for some reason got this error.

An unhandled exeption of type 'System.NullReferenceException' occurred 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
"EmbedTTF.vb.Radio___.TTF" did not work since the fontStream was "Nothing"
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's value is "Nothing"??

Any ideas?

'load the resource

Dim fontStream As Stream =
Me.GetType().Assembly.GetManifestResourceStream("E mbedTTF.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 on
embedding true type fonts in Windows Forms Tips and Tricks.

--
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" <Bo********@discussions.microsoft.comwrote in message
news:B5**********************************@microsof t.com...

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



Nov 14 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.