Question Need to use Resource Embedded font in Application

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
143
Programming Experience
10+
I have a Font that I want my application to use. I have tried MANY suggestions on the web and have not found one that works. I tried embedding the file in the Application's Resources, but could never get that to work. I also tried to get ClickOnce to install the font for me, but also couldn't get that to work. Is this not something very commonly done? Anyway if anyone any suggestions, I would appreciate it.
 
The above is if you are using GDI.

Below is if you are using DirectWrite:
 
I tried AddFontMemResourceEx but am having problems with the fontStream. It comes back null.

C#:
Expand Collapse Copy
        [System.Runtime.InteropServices.DllImport("gdi32.dll")] private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, ref uint pcFonts);

            try
            {
                string resource = "LEDFont.ttf"; // specify embedded resource name

                // receive resource stream
                System.IO.Stream fontStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);

                // create an unsafe memory block for the font data
                System.IntPtr data = System.Runtime.InteropServices.Marshal.AllocCoTaskMem((int)fontStream.Length);

                // create a buffer to read in to
                byte[] fontdata = new byte[fontStream.Length];

                // read the font data from the resource
                fontStream.Read(fontdata, 0, (int)fontStream.Length);

                // copy the bytes to the unsafe memory block
                System.Runtime.InteropServices.Marshal.Copy(fontdata, 0, data, (int)fontStream.Length);

                ///IMPORTANT line to register font in system
                uint cFonts = 0;
                AddFontMemResourceEx(data, (uint)fontdata.Length, IntPtr.Zero, ref cFonts);

                // pass the font to the font collection
                pFC.AddMemoryFont(data, (int)fontStream.Length);

                // close the resource stream
                fontStream.Close();
                // free up the unsafe memory
                System.Runtime.InteropServices.Marshal.FreeCoTaskMem(data);

                lblTaktTime.Font = new Font(pFC.Families[0], 60, FontStyle.Bold);
            }
            catch (Exception exp)
            {
            }

1755638884654.png


Any ideas?
 
That would suggest that the resource is not named what you think it is. E.g. Line 5 las the wrong value.
 
As you can see from your screenshot, the resource is named "LEDFont", not "LEDFont.ttf". Adding resources the way you have will always drop the file extension.

I'm still not sure that you'll be able to access that resource that way though. You'll probably need to use My.Resources to get a Stream. If you want to use GetManifestResourceStream then I think you'll have to add the file to your project and change its Build Action to Embedded Resource. Not 100% sure about that though.
 
Back
Top Bottom