Solved

Enumerate embedded fonts in my.resources

Posted on 2015-01-03
11
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 63

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 63

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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 63

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
 

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 63

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 63

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 63

Expert Comment

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

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

751 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