Solved

Enumerate embedded fonts in my.resources

Posted on 2015-01-03
11
130 Views
Last Modified: 2015-01-08
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?
0
Comment
Question by:Basicfarmer
  • 6
  • 5
11 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40529294
Hi Basicfarmer;

Having a collection of fonts you can use the Font.Name property to identify the Font.
0
 

Author Comment

by:Basicfarmer
ID: 40529334
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40529437
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
 

Author Comment

by:Basicfarmer
ID: 40529601
Im not sure how to access one drive. I could zip the resources folder would that help.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40529627
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Basicfarmer
ID: 40530654
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40530699
Please show how you load the resource files into your program.
0
 

Author Comment

by:Basicfarmer
ID: 40538455
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40538872
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
 

Author Closing Comment

by:Basicfarmer
ID: 40538881
Perfect, Thank you...
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40538888
Not a problem, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now