Solved

Enumerate embedded fonts in my.resources

Posted on 2015-01-03
11
128 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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.

705 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

20 Experts available now in Live!

Get 1:1 Help Now