Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have come across a strange issue whilst embedding a font into a custom user control. This is the code i used to store the font in order to use it again at runtime.

 private PrivateFontCollection pfc;            // Stores the font used by the control
 
private void GeneratePrivateFontCollection()
      {
         // Load the resource into font stream
         Stream fontStream = new MemoryStream(Renishaw.Calibration.RotaryXL.UI.Properties.Resources.digital_7__mono_);

         if (fontStream != null)
         {
            // Remember to set project properties (build) to allow unsafe code
            unsafe
            {
               // 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;
               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);

               // 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);
            }
         }
      }

 private void SetControlFont()
      {
         FontStyle fs = this.Font.Style;

         // Serch through private font collection
         if (pfc.Families.Length > 0)
         {
            foreach (FontFamily ff in pfc.Families)
            {
               // Find the appropriate style and set the controls font to use it
               if (ff.IsStyleAvailable(FontStyle.Regular) && fs == FontStyle.Regular)
               {
                  m_font = new Font(ff, m_fontSize, FontStyle.Regular);
                  return;
               }
            }
         }
      }


The problem comes when drawing the font in the control

protected override void OnPaint(PaintEventArgs e)
{
   base.OnPaint(e);
   Graphics g = e.Graphics;
   Brush readingBrush = new SolidBrush(m_textColor);
   g.DrawString(m_reading, m_font, readingBrush, m_location);
   readingBrush.Dispose();
}


This all works fine on my PC and another one in the office. However 3 of the PC's for some reason do not use the embedded font. Could anyone think of a reason for this?

Thanks George
Posted
Comments
ely_bob 22-Jul-10 11:26am    
Are the Graphics cards the same? on each of the machines? this looks like an issues with GPU settings...

1 solution

Hi,
Sorry for stupid question, but have you actually initialized 'pfc' ?

Also, make sure to call SetCompatibleTextRenderingDefault(true); or override UseCompatibleTextRendering property in your control and return true.

One last thing that comes to my mind is the font itself: does it exist on those PCs ?

Cheers.
 
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