Using a font embedded as a resource?
Using a font embedded as a resource?
Using a font embedded as a resource?
Using a font embedded as a resource?
Using a font embedded as a resource?
Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource?
Using a font embedded as a resource? Using a font embedded as a resource?
Using a font embedded as a resource?
Go Back  Xtreme Visual Basic Talk > > > Using a font embedded as a resource?


Reply
 
Thread Tools Display Modes
  #1  
Old 01-31-2006, 01:25 AM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Question Using a font embedded as a resource?


I *hope* this is the right place to post this.

I have a TrueType font that I use in my app that I don't want to have to install seperately on every machine that uses the app. Recently I got some help from some nice folks here on figuring out how to embed a sound file properly so I could use it with the My.Resources namespace. Is there a similar method (or hell, ANY method) that I could use to access a .ttf file that I have embedded in my project? I'm not trying to infringe any copyrights, I know the person who made the font in question, in fact, and it's legal for distribution. I suppose I _could_ package it if I had to, but I'd rather not.

Thanks for any help!
Reply With Quote
  #2  
Old 01-31-2006, 06:49 AM
shaul_ahuvaUsing a font embedded as a resource? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

I don't know if there's a better .NET 2.0 way to do it, but you can load the font into memory. I've never done it, but here's an article on how to add a memory font and then use it in an application...
Reply With Quote
  #3  
Old 01-31-2006, 12:02 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
I don't know if there's a better .NET 2.0 way to do it, but you can load the font into memory. I've never done it, but here's an article on how to add a memory font and then use it in an application...
Yeah, I've tried that and I can't get it to work, which is one reason I was hoping against hope there was a better way. When I follow their directions, and then put the font call in Public Sub New, like this:
Code:
Me.Clock.Font = New System.Drawing.Font(FntFC.Families(0), 48.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
...I get a "Name 'FntFC' Is Not Declared" error, which to me is wacky, because I see it right there in the function code. Probably doesn't help that I have no clue at all as to how the code works.
Reply With Quote
  #4  
Old 01-31-2006, 12:51 PM
shaul_ahuvaUsing a font embedded as a resource? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

FntFC (in their example) is an instance of the PrivateFontCollection type. You would need to get an instance of the PrivateFontCollection type by calling GetFont.

Code:
Dim FntFC As PrivateFontCollection = GetFont(...) Me.Font = New Font(FntFC.Families(0), 10)
Reply With Quote
  #5  
Old 01-31-2006, 01:05 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
FntFC (in their example) is an instance of the PrivateFontCollection type. You would need to get an instance of the PrivateFontCollection type by calling GetFont.

Code:
Dim FntFC As PrivateFontCollection = GetFont(...) Me.Font = New Font(FntFC.Families(0), 10)
I thought that might be missing, but I figured since it was called inside of the function, it was there. I should know better.

I don't understand what's supposed to replace the ellipsis, though. Is that where I put the name of the embedded font file? Given the name of the font file is "eggcrate.ttf", how would I complete that?
Reply With Quote
  #6  
Old 01-31-2006, 04:44 PM
shaul_ahuvaUsing a font embedded as a resource? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

Going by the example, GetFont is expecting an array of resource names without the assembly's namespace...so you would pass in a single element array containing "eggcrate.ttf".
Reply With Quote
  #7  
Old 01-31-2006, 04:48 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
Going by the example, GetFont is expecting an array of resource names without the assembly's namespace...so you would pass in a single element array containing "eggcrate.ttf".
Which you would then declare seperately, I gather. I guess I'll have to read up on arrays.

(I'm REALLY new at VB, I've been playing with it for a week or two and I'm trying to port over what little of .NET I learned when I was dabbling in VC++.NET and what I remember from taking Pascal in high school fifteen years ago. So I greatly appreciate your patience with what I'm sure are really stupid questions. )
Reply With Quote
  #8  
Old 01-31-2006, 05:10 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Alright, so here's where I am:

I have declared the following:
Code:
Dim FontArray() As String = {"Eggcrate.ttf"} Dim FntFC As Drawing.Text.PrivateFontCollection = GetFont(FontArray)
...and I call the font the same way I did before.

So now when I try to compile the app, it throws an unhandled exception, the details of which are utter gibberish to me, but I'll paste it if you think it might help. The important part seems to be "Object reference not set to an instance of an object.".
Reply With Quote
  #9  
Old 01-31-2006, 05:30 PM
shaul_ahuvaUsing a font embedded as a resource? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

What line is the exception thrown on? It should give you line numbers in the exception's stack trace.
Reply With Quote
  #10  
Old 01-31-2006, 05:36 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
What line is the exception thrown on? It should give you line numbers in the exception's stack trace.
I'm guessing they work different in VS2005, or else I just don't know what you mean.

Here's what it copies to the clipboard:
Code:
System.InvalidOperationException was unhandled Message="An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object." Source="Inquizitor 2005" StackTrace: at Inquizitor_2005.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190 at Inquizitor_2005.My.MyProject.MyForms.get_MainForm() at Inquizitor_2005.My.MyApplication.OnCreateMainForm() in C:\Documents and Settings\FSmythe\My Documents\Visual Studio 2005\Projects\Inquizitor 2005\My Project\Application.Designer.vb:line 35 at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at Inquizitor_2005.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
Reply With Quote
  #11  
Old 01-31-2006, 05:41 PM
shaul_ahuvaUsing a font embedded as a resource? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

The best thing I would suggest to do would be to step through the code, keeping an eye on your local variables list. When the exception is thrown, you'll get a dialog window indicating where the exception occurred.

From there, you can determine which variable is Nothing (null reference exceptions are thrown when a variable set to Nothing is accessed).
Reply With Quote
  #12  
Old 01-31-2006, 05:52 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
The best thing I would suggest to do would be to step through the code, keeping an eye on your local variables list. When the exception is thrown, you'll get a dialog window indicating where the exception occurred.

From there, you can determine which variable is Nothing (null reference exceptions are thrown when a variable set to Nothing is accessed).
Gotcha. (I think.) It seems to be inside of the GetFont function, which it wayyyyy over my head. Happens at this line:
Code:
Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte
So I gotta think that there's something different about how all this works between VB.NET and VB2005. So until someone can help me or someone can find a better way to do it in 2005, I think I'm screwed.

(I DO notice this: There are mentions to IO.Stream in the FntStrm declaration. The sounds I embedded seem to embed as type System.IO.MemoryStream, whereas the font embedded as System.Byte[]. I have NO idea if this is meaningful.)
Reply With Quote
  #13  
Old 01-31-2006, 07:25 PM
shaul_ahuvaUsing a font embedded as a resource? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

It looks like FntStrm is not referencing an object. The reasons the exception is occuring can be due to:
  • The font isn't flagged as an embedded resource
  • The call to GetManifestResourceStream is being given the wrong namespace and resource name.
Assuming everything is taking place in the same assembly (application), you should be okay just passing the embedded file's name. To check the resource names you can either do a debug call to [Assembly].GetExecutingAssembly().GetManifestResourceNames() or use Reflector.

Streams deals strictly with bytes, so there's no difference - the byte array's length should be the same as the stream's length. The only time issues with byte array length arise is when you're dealing with text in an encoding that uses (or can use) more than 1 byte per character (such as Unicode or UTF8).
Reply With Quote
  #14  
Old 01-31-2006, 07:31 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Yeah, I'm officially lost. I appreciate the advice and help, but you're way over my head.

I'll see what this Reflector thing does and if I can figure it out, but right now I'm thinking maybe I'm biting off more than I can chew.
Reply With Quote
  #15  
Old 02-01-2006, 04:06 AM
shaul_ahuvaUsing a font embedded as a resource? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

Quote:
Originally Posted by clemon79
Yeah, I'm officially lost. I appreciate the advice and help, ut you're way over my head.

I'll see what this Reflector thing does and if I can figure it out, but right now I'm thinking maybe I'm biting off more than I can chew.
What part of my response don't you understand? This is a resource loading problem (i.e. the font can't be found in your application), and there's only a few possible errors that can cause of the problem. Excluding the code in GetFont, it should be relatively simple to get the problem resolved.

If you want to attach the project (without the executable), I (or someone else) could take a quick look at it for you...

Last edited by shaul_ahuva; 02-01-2006 at 04:14 AM.
Reply With Quote
  #16  
Old 02-01-2006, 03:52 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
What part of my response don't you understand? This is a resource loading problem (i.e. the font can't be found in your application), and there's only a few possible errors that can cause of the problem. Excluding the code in GetFont, it should be relatively simple to get the problem resolved.

If you want to attach the project (without the executable), I (or someone else) could take a quick look at it for you...
Yeah, it's certainly not your fault, it's all me, I just don't know enough VB and development stuff to understand all of the technical stuff.

So I'm not sure what not to attach, beyond the "bin" folder. Should I cull out that and the "obj" folder, or will that break things? Those seem to be the folders with executables in them.
Reply With Quote
  #17  
Old 02-02-2006, 06:53 AM
shaul_ahuvaUsing a font embedded as a resource? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

Everything in the bin and obj folder are generated when you build the application - they're only used at run time. The only things needed are the project file(s) and source file(s) (excepting copyrighted things like your font file...).

I would definitely suggest checking out Visual Basic.NET Books if you want to learn more about .NET.
Reply With Quote
  #18  
Old 02-02-2006, 02:25 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
Everything in the bin and obj folder are generated when you build the application - they're only used at run time. The only things needed are the project file(s) and source file(s) (excepting copyrighted things like your font file...).

I would definitely suggest checking out Visual Basic.NET Books if you want to learn more about .NET.
Excellent. I've attached the project sans both folders. (The font, while it mentions a 1999 copyright date, is openly downloadable for public use, and I know the guy who made it, so I feel comfortable leaving it in the structure.) I appreciate the help.

And I have Halverson's Step-By-Step book for 2005, I look forward to getting some time to pour through it.
Attached Files
File Type: zip Inquizitor 2005.zip (239.3 KB, 19 views)
Reply With Quote
  #19  
Old 02-02-2006, 08:49 PM
shaul_ahuvaUsing a font embedded as a resource? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

I saw the problem right away Your assembly's default namespace has a space in it. This is replaced with an underscore.

You'll have to either remove the space from the namespace, or modify the NameSpc variable in GetFont to replace spaces with underscores (I would recommend changing the namespace).
Reply With Quote
  #20  
Old 02-02-2006, 09:38 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
I saw the problem right away Your assembly's default namespace has a space in it. This is replaced with an underscore.

You'll have to either remove the space from the namespace, or modify the NameSpc variable in GetFont to replace spaces with underscores (I would recommend changing the namespace).
Okay. I don't know what this means. (Well, I do, but vaguely.) Are you saying that calling the project "Inquizitor 2005" is creating the problem, and renaming it to "Inquizitor2005" will fix it?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Using a font embedded as a resource?
Using a font embedded as a resource?
Using a font embedded as a resource? Using a font embedded as a resource?
Using a font embedded as a resource?
Using a font embedded as a resource?
Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource? Using a font embedded as a resource?
Using a font embedded as a resource?
Using a font embedded as a resource?
 
Using a font embedded as a resource?
Using a font embedded as a resource?
 
-->