Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
3.86/5 (3 votes)
See more:
Hi,I used this class to embed a font
C#
public static class theFont
    {
        [DllImport("gdi32.dll")]
        private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
       static FontFamily ff;
       static Font font;

        public static Font CargoPrivateFontCollection(float size,FontStyle style)
        {
            // Create the byte array and get its length

            byte[] fontArray = Salary.Properties.Resources.MyFont;
            int dataLength = Salary.Properties.Resources.MyFont.Length;


            // ASSIGN MEMORY AND COPY  BYTE[] ON THAT MEMORY ADDRESS
            IntPtr ptrData = Marshal.AllocCoTaskMem(dataLength);
            Marshal.Copy(fontArray, 0, ptrData, dataLength);


            uint cFonts = 0;
            AddFontMemResourceEx(ptrData, (uint)fontArray.Length, IntPtr.Zero, ref cFonts);

            PrivateFontCollection pfc = new PrivateFontCollection();
            //PASS THE FONT TO THE  PRIVATEFONTCOLLECTION OBJECT
            pfc.AddMemoryFont(ptrData, dataLength);

            //FREE THE  "UNSAFE" MEMORY
            Marshal.FreeCoTaskMem(ptrData);

            ff = pfc.Families[0];
           return font = new Font(ff,size, style);
        }
    }

I'm going to use this font in all Controls in forms so I tried to put below code in Load event of all Forms and UCs
C#
/////////////Font///////////
   foreach (Control cx in this.Controls)
       cx.Font = theFont.CargoPrivateFontCollection(9f, FontStyle.Regular);

but this error in Program.cs happend
C#
"system.drawing.Graphics Object is currently in use elsewhere"


What's the problem?
Posted
Updated 9-May-14 1:36am
v3
Comments
mit62 9-May-14 8:27am    
don't know anyone?
agent_kruger 9-May-14 10:16am    
sir, in which line does this error appear?
mit62 10-May-14 1:12am    
in Program.cs file on Application.run
agent_kruger 10-May-14 1:15am    
sir, where is the error showing in the "theFont" class.
mit62 10-May-14 2:30am    
no error in "theFont" class.But just if I use this code
foreach (Control cx in this.Controls)
cx.Font = theFont.CargoPrivateFontCollection(9f, FontStyle.Regular);

in more than one of User controls this error happen.

(plus I have some problem on this foreach loop,because it dosn't select all controls in UserConrol)

This is a really bad idea to use P/Invoke where it is not absolutely needed, because it can totally break that powerful platform compatibility CLR provides. Why using it with resources? There are .NET resources embedded in the module of an assembly; they work perfectly for absolutely any kind of resources, including any custom data, regardless of the data format.

As to the embedding of fonts, this is well demonstrated in this CodeProject article:
Embedding Font To Resources[^].

—SA
 
Share this answer
 
Comments
mit62 10-May-14 1:14am    
Thanks for your reply.in my solution I embed font to resources too.
Sergey Alexandrovich Kryukov 10-May-14 19:12pm    
I know. This is what I'm talking about: you do embed fonts, but I'm arguing against the use of P/Invoke.
—SA
sir, if you debug it correctly you will see the error is generated from that class named "theFont" . Try using "try catch" to check where is the error generating.
 
Share this answer
 

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