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
  #21  
Old 02-02-2006, 10:43 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default


Wait, no, I got it, I found where the Assembly name was, and fixed it to get rid of the space, and it seems to be running now!

Thank you SO much for the help. There is no way in nine blue hells I would have found that myself. I really appreciate it and all of your patience.
Reply With Quote
  #22  
Old 02-03-2006, 12:18 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
Wait, no, I got it, I found where the Assembly name was, and fixed it to get rid of the space, and it seems to be running now!

Thank you SO much for the help. There is no way in nine blue hells I would have found that myself. I really appreciate it and all of your patience.
Not a problem
Reply With Quote
  #23  
Old 02-14-2006, 01:45 AM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Okay, I hate to regurgitate this, but something came up with regard to this vis a vis a different project.

So I know I can embed a font this way if I include that GetFont function inside of the form in which I want to use the embedded font file.

I have another project that uses a number of different forms, and I need to use the same font in several of them. Is there a way I can rig this so that the GetFont function code only has to appear once and all of the necessary forms can call it? Is this something you would add a new Class to do? Or maybe a Module? (And if so, how do I do it?) Or would I need to include that function code in every form that used it? I can do that (it's only three of them, I think), but that seems awfully bloaty and I gotta think there's a better way.

(I tried making a new Module, and pasted the GetFont code verbatim, just to see what would happen, and I'm getting the error "Name 'AddFontMemResourceEx' is not declared". Do I need to tell it to look in the gdi32 library for it? If so, how do I do that?)

Thanks again for the help!

Last edited by clemon79; 02-14-2006 at 02:32 AM.
Reply With Quote
  #24  
Old 02-14-2006, 09:14 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

If you're going to re-use the code, I would definitely move it to a separate class. You could make the method Shared so you don't have to instatiate a class just to call the method (Yes, I know a Module can do the same thing but - Yech!).

As far as your error, you would definitely need to move the imported function along with the GetFont code.

I would probably do something like:

Code:
Public Class FontSupport Private Shared _EmbeddedFonts As FontCollection Private Sub New() End Sub 'AddFontMemResourceEx declaration... 'GetFont implementation... Public Shared ReadOnly Property EmbeddedFonts() As FontCollection Get If FontSupport._EmbeddedFonts Is Nothing Then FontSupport._EmbeddedFonts = FontSupport.GetFont(...) End If Return FontSupport._EmbeddedFonts End Get End Property End Class

(I smell a My namespace addition here...)

To access the embedded fonts, just access "FontSupport.EmbeddedFonts"...
Reply With Quote
  #25  
Old 02-14-2006, 01:00 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
As far as your error, you would definitely need to move the imported function along with the GetFont code.
Okie, I'm lost again. I don't know how to do this. All I know about AddFontMemResourceEx is that it is part of the GDI32 library. Which means I don't know what to do about this line:
Code:
'AddFontMemResourceEx declaration...
In fact, here's what I did:
Code:
Public Class FontSupport Private Shared _EmbeddedFonts As Drawing.Text.FontCollection Private Shared FontArray() As String = {"Eggcrate.ttf"} Private Sub New() End Sub 'AddFontMemResourceEx declaration... Public Function GetFont(ByVal FontResource() As String) As _ Drawing.Text.PrivateFontCollection 'Get the namespace of the application Dim NameSpc As String = _ Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString() Dim FntStrm As IO.Stream Dim FntFC As New Drawing.Text.PrivateFontCollection() Dim i As Integer For i = 0 To FontResource.GetUpperBound(0) 'Get the resource stream area where the font is located FntStrm = _ Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( _ NameSpc + "." + FontResource(i)) 'Load the font off the stream into a byte array Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte FntStrm.Read(ByteStrm, 0, Int(CType(FntStrm.Length, Integer))) 'Allocate some memory on the global heap Dim FntPtr As IntPtr = _ Runtime.InteropServices.Marshal.AllocHGlobal( _ Runtime.InteropServices.Marshal.SizeOf(GetType(Byte)) * _ ByteStrm.Length) 'Copy the byte array holding the font into the allocated memory. Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, _ FntPtr, ByteStrm.Length) 'Add the font to the PrivateFontCollection FntFC.AddMemoryFont(FntPtr, ByteStrm.Length) Dim pcFonts As Int32 pcFonts = 1 AddFontMemResourceEx(FntPtr, ByteStrm.Length, 0, pcFonts) 'Free the memory Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr) Next Return FntFC End Function Public Shared ReadOnly Property EmbeddedFonts() As Drawing.Text.FontCollection Get If FontSupport._EmbeddedFonts Is Nothing Then FontSupport._EmbeddedFonts = FontSupport.GetFont(FontArray) End If Return FontSupport._EmbeddedFonts End Get End Property End Class
...and it also spits out a "Reference to a non-shared member requires an object reference." in line 48. But I _did_ declare it as shared...didn't I?
Quote:
(I smell a My namespace addition here...)
Yeah, ya think? I've been shocked since the start of this that it wasn't part of it in the first place, it seems like there would be a lot of call for this sort of thing.

As always, thanks for the help.
Reply With Quote
  #26  
Old 02-14-2006, 01:17 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

GetFont isn't declared as Shared.

As far as AddFontMemResourceEx, just move the function declaration (Private Declare Auto Function AddFontMemResourceEx...) from the form where you had it into FontSupport; that should do the trick
Reply With Quote
  #27  
Old 02-14-2006, 01:22 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by shaul_ahuva
GetFont isn't declared as Shared.

As far as AddFontMemResourceEx, just move the function declaration (Private Declare Auto Function AddFontMemResourceEx...) from the form where you had it into FontSupport; that should do the trick
Oh jeez, I'm a dingbat, I completely overlooked that.

And, yes, adding "Shared" to the GetFont declaration seems to have cleared up that other error. Now we just have to see if all of this actually WORKS.

I may be back. I'll keep ya posted either way.
Reply With Quote
  #28  
Old 02-14-2006, 03:24 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by clemon79
I'll keep ya posted either way.
Okay, I got it working. Problem seems to be that the project I'm trying to add this functionality to started out life as a VB2003 app, and the thingy that converts it to VB2005 doesn't do it very well, meaning it doesn't automatically build a resource file the app can see and whatnot, which is creating the same runtime error I got before about not being able to find the file. So I think I need to rebuild this in VB2005, which shouldn't be TOO hard since I have all of the source and it works, I just need to rebuild the forms. I can prolly do it in an hour or two.

So I'm good for now, I think. At least, now, I know how to port this functionality over at future apps. Thanks again!

(so who do we have to kill to just get a stinkin' My.Resources.Fonts? It would have made this thing SO much easier.)
Reply With Quote
  #29  
Old 02-14-2006, 11: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

You'll probably want to package the code as a class library, then just reference it from apps you make in the future - I usually make a "common" class library containing everything I typically use and just reference that in all of my projects. It's a lot easier (and much less error-prone) to distribute the functionality that way

As far as My.Resources.Fonts, I don't think there's an easy way to scan an entire AppDomain for just font files (plus they don't necessarily need to have a .ttf extension, and even if they do it doesn't mean the resource is a font) outside of Assembly.GetManifestResourceNames. I'll do some checking around, and see if I can cook something up
Reply With Quote
  #30  
Old 06-09-2007, 02:17 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Your worst nightmare is back!

So I picked this project back up after letting it sit for a long time, and I've discovered that the FontSupport method we've discussed above doesn't work in Windows Vista.

I have, however, been directed to a new method, at

http://www.bobpowell.net/embedfonts.htm

...which appears to be much simpler. Mind, I still don't understand it, but I at least have a better chance to one day.

Ideally I'd eventually like to get this set up as a class like we did above, to get the code out of the MainForm code, but for now I have it as MainForm_Load. Which may or may not be right.

Anyhow, when I run it, I get the "Object reference not set to an instance of an object" error again. Which I'm guessing means I'm not referring to the embedded resource correctly.

I've attached the project...could someone take a look and maybe figure out where I'm going wrong?

Also, again, ideally I'd like to set this up as a class, so if someone could help me do that, I'd be most appreciative.

I warn everyone again that I'm pretty much a VB 'tard, so I throw myself on the mercy of the court.

Thanks for any help!
Attached Files
File Type: zip Inquizitor_2005.zip (256.4 KB, 14 views)
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?
 
-->