473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Private Font Collection – writing with private fonts


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
6 8072
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bats | last post by:
Hello. On Windows, I used to have a little app that would let me navigate visually through my 20,000+ fonts and I could create groups of them for quick install/remove from the system. It was great...
9
by: Zenobia | last post by:
Is there a problem with the CSS font-family property with IE6 - or is it my understanding of the font-family property? I was under the impression that the browser tried to use the first font. If...
133
by: Philipp Lenssen | last post by:
Why is there no standardized and well-working way for a web-page to offer the font for download/embed it, in order to be displayed on the page? No matter what you think of the preferred font of a...
9
by: Adam | last post by:
Can someone please help!! I am trying to figure out what a font is? Assume I am working with a fixed font say Courier 10 point font Question 1: What does this mean 10 point font Question 2:...
10
by: Sylvain Audet | last post by:
Hello! We have a Windows application that is using Crystal Reports reports containing Barcode fonts. Those reports are called through reflection into a Crystal Report Viewer and we need to have...
3
by: al jones | last post by:
Okay, last go 'round for today. I'm looking at the msdn for private font collections. If I add a font to work with - let's presume I'm printing a single line in a particular font and won't use it...
1
by: CMiner | last post by:
What I am trying to do is this: Let a user browse for font files that are not installed, select one, and show a sample of the font in a text box (That way the user can change the size, style, and...
14
by: Roedy Green | last post by:
Is there a shortcut way to define the default font family (and characteristics) to be applied to all styles? -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
3
by: Sin Jeong-hun | last post by:
Windows comes with many fonts. When I'm writing with a word processor, it's always annoying that fonts of the languages which I can't even read (such as arabic) fill the font list making it hard...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.