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.
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...
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.
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)
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?
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".
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. )
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.".
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()
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).
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.)
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).
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.
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.
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.
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.
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).
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?
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