Enumerate embedded fonts in my.resources

Experts, I have 6 fonts in my resources that I need to get into a PrivateFontCollection. I can enumerate all the items in my resources but I cannot figure out how to identify a font. Can anyone help me with this?
BasicfarmerAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi Basicfarmer;

I have found out that loading fonts using the Resource file as you have done is not supported. I did find some resources on the Internet on how to accomplish this, please see this article, Embedding Font To Resources, which I translated to VB .Net posted below

Then once the collection of fonts have been loaded you can load a control with one of the fonts like so.

' Using a label control
Label1.Font = New Font(MyFontCollection.Families(0), 12)
Label1.UseCompatibleTextRendering = True

The MyFontCollection can not be iterated over so if you want to know what font name is in a slot of the collection use a for loop and index into the collection and look and the Name property.

MyFontCollection.Families(i).Name  ' Where i is a zero base index into the collection

To return the name of the font.

Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Drawing.Text

Private MyFontCollection As New PrivateFontCollection

Private Sub LoadFont(ByVal filename As String)
    '// specify embedded resource name
    Dim resource As String = "WindowsApplication4." & filename

    '// receive resource stream
    Dim fontStream As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource)

    '// create an unsafe memory block for the font data
    Dim data As System.IntPtr = Marshal.AllocCoTaskMem(CType(fontStream.Length, Integer))

    '// create a buffer to read in to
    Dim fontdata(fontStream.Length) As Byte

    '// read the font data from the resource
    fontStream.Read(fontdata, 0, CType(fontStream.Length, Integer))

    '// copy the bytes to the unsafe memory block
    Marshal.Copy(fontdata, 0, data, CType(fontStream.Length, Integer))

    '// pass the font to the font collection
    MyFontCollection.AddMemoryFont(data, CType(fontStream.Length, Integer))

    '// close the resource stream
    fontStream.Close()

    '// free up the unsafe memory
    Marshal.FreeCoTaskMem(data)
End Sub

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi Basicfarmer;

Having a collection of fonts you can use the Font.Name property to identify the Font.
0
 
BasicfarmerAuthor Commented:
Here is where I need to identify the font to get the fonts into the collection. This is what I have been testing. In my tests I only have 1 image file and 6 font files in the resources. So I can say "IF NOT AN IMAGE". But in my application I have tons of resources of many types. What I need to be able to say is "IF IT IS A FONT".

 
            pfc = New PrivateFontCollection

            Dim runTimeResourceSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, True, True)
            Dim ItemEnumerator As System.Collections.IDictionaryEnumerator = runTimeResourceSet.GetEnumerator

            While ItemEnumerator.MoveNext
                If Not TypeOf (ItemEnumerator.Value) Is Image Then
                    Dim fontData() As Byte = ItemEnumerator.Value
                    Dim fontPtr As IntPtr = Marshal.AllocHGlobal(fontData.Length)
                    Marshal.Copy(fontData, 0, fontPtr, fontData.Length)
                    'Add the font to the shared collection of fonts:
                    pfc.AddMemoryFont(fontPtr, fontData.Length)
                    Marshal.FreeHGlobal(fontPtr)
                End If
            End While

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Fernando SotoRetiredCommented:
Can you zip the and post the project to Microsoft OneDrive. Got to see whats in the resource. Post download like here when your done.
0
 
BasicfarmerAuthor Commented:
Im not sure how to access one drive. I could zip the resources folder would that help.
0
 
Fernando SotoRetiredCommented:
The Microsoft OneDrive website is run by Microsoft and you can get free access by signing up for a free account which will allow you to upload and down load files. Please check out the above link.
0
 
BasicfarmerAuthor Commented:
Here is the link. I do not yet have all of the files in the resources folder that will be there. I will have most all types of cad and 3D model files in the resources file when the application is completed. Currently there are most image files.

https://onedrive.live.com/redir?resid=8D9B5939254E95D6%21106
0
 
Fernando SotoRetiredCommented:
Please show how you load the resource files into your program.
0
 
BasicfarmerAuthor Commented:
I apologize I missed your request. I usually rely on emails received when a comment has been posted but I did not notice anything.

I am loading the resources through the project properties.

Screen Shot
0
 
BasicfarmerAuthor Commented:
Perfect, Thank you...
0
 
Fernando SotoRetiredCommented:
Not a problem, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.