Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've downloaded several custom fonts. When the user presses a button I want the font of the text in a rich-textbox to change. But the fonts themselves don't change but the font size does. I also don't get any errors. When I install the font on my PC the font works perfectly. I can then also use it as the default font using the VS properties editor and can assign the font using both the codes below. But when I de-install the font it stops working again. It seems as if VS doesn't recognize the font even when using a direct directory link and just skips over/ignores the font part and just adjusts the size of the normal font. I have it working on labels but not on rich-textboxes.

I've looked at a lot of related questions on this forum and elsewhere but none of them worked. I would like to use custom fonts that the user doesn't have to install. I've also put the fonts in the resources using Resources.resx and set them to public.

Sorry if this is a stupid or easily solved question but I just can't get it to work on rich/textboxes.

What I have tried:

This is what I've tried so far:
C#
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(@"C:\\Users\\MyName\\Documents\\Font\\6937\\HARRYP.ttf");
System.Drawing.Font font = new Font(pfc.Families[0], 12);
richTextBox_Translate.Font = font;


And any permutation of this:
C#
richTextBox_Translate.Font = new Font(@"C:\\Users\\Myname\\Documents\\Font\\6937\\HARRY P", 12);
richTextBox_Translate.Font = new Font(@".\\Resources\\HARRY P", 12);
richTextBox_Translate.Font = new Font("HARRY P", 12);


The font does change when I use a standard font±
C#
richTextBox_Translate.Font = new Font("Comic Sans MS", 12);


And I found (several) of these but they don't work either:
C#
[DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
PrivateFontCollection pFC = new PrivateFontCollection();

        private void LoadFont()
        {
            Stream fontStream = new MemoryStream(Properties.Resources.DBSScouter);
            //create an unsafe memory block for the data
            System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);
            //create a buffer to read in to
            Byte[] fontData = new Byte[fontStream.Length];
            //fetch the font program from the resource
            fontStream.Read(fontData, 0, (int)fontStream.Length);
            //copy the bytes to the unsafe memory block
            Marshal.Copy(fontData, 0, data, (int)fontStream.Length);

            // We HAVE to do this to register the font to the system (Weird .NET bug !)
            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 the unsafe memory
            Marshal.FreeCoTaskMem(data);

            System.Drawing.Font font = new Font(pFC.Families[0], 12);
            richTextBox_Translate.Font = font;
        }
Posted
Updated 1-Mar-20 4:12am
Comments
Richard MacCutchan 29-Feb-20 9:34am    
"But when I de-install the font it stops working again."
Surely that is what you would expect?
db211086004 4-Mar-20 10:29am    
Yes and no because I'm trying to use a custom font with a set path which doesn't work but after installing the font on the machine (which is something I don't want to do) it does work. I only mentioned that de-installing it stops it from working again because I wanted to give as much info as possible so that it was easier to come up with an answer.

1 solution

You probably need to use the Graphics.Drawstring() method to be able to use these fonts, see example here: How to: Create a Private Font Collection - Windows Forms | Microsoft Docs[^]
Also see: Advanced Typography | Colors, Fonts, and Text[^]
 
Share this answer
 
v2
Comments
db211086004 4-Mar-20 10:24am    
No does the same thing. The editor clearly show that fontName becomes the correct custom font but then when a new Font is created to use in the DrawString it sets the fontFamily to a standard font.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900