locked
ClearType and TextRenderer RRS feed

  • Question

  • Hi,


    I have a problem where ClearType blur the text, written with TextRenderer.DrawText, to a point where it is almost unreadable.

    How do i temporary disable or suppress ClearType when drawing text ??


    Thank you in advance.

    BR

    Peter
    Thursday, July 22, 2010 2:30 PM

Answers

  • Hi Peter,

    The Bitmap constructor has an overload that takes a PixelFormat parameter. Have you tried setting it to e.g. PixelFormat.Format32bppRgb?

    Also: 

    using (Graphics g = Graphics.FromImage(result))
    {
     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
     g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
     // do the fill stuff;
    }
    
    

    Automatic scalling done in g.DrawImage() can also make artefacts apear in the final picture (as do many other factors, e.g. AutoSize=true in an PictureBox). Depends on your code.

    P.S. Try setting g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit if you want to use TextRenderer.DrawText without those black ClearType outlines. At least on my current system (XP, SP3) this works fine.

    Marcel

    Thursday, July 22, 2010 5:42 PM

All replies

  • Hi,

    This sometimes happens when there is no background set, so no antialising is possible. I don't know if this is your case, but have a look here and see how Graphics.DrawString is used instead of TextRenderer.DrawText to obtain better results.

    Marcel

     

    Thursday, July 22, 2010 2:50 PM
  • Hi Marcel,

    Well, I already use Graphics.DrawString as an alternative solution when ClearType is active (SystemInformation.FontSmoothingType), but it has fewer options and doesn't always look so good as TextRenderer does - plus it has some limitations when mixing UserPaint with standard controls on systems using cleartype.

    Transparent background could be some of the answer, since it does work sometimes.

    If the Graphics is delivered from a UserControl.OnPaint, it works. If it is taken from a BitMap, it fails.

    The bitmap graphics, is created like this:

     

            result = new Bitmap(client.Width, client.Height);
            using (Graphics g = Graphics.FromImage(result))
            {
              //do the fill stuff;
            }


    BR

    Peter

    Thursday, July 22, 2010 4:45 PM
  • Hi Peter,

    The Bitmap constructor has an overload that takes a PixelFormat parameter. Have you tried setting it to e.g. PixelFormat.Format32bppRgb?

    Also: 

    using (Graphics g = Graphics.FromImage(result))
    {
     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
     g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
     // do the fill stuff;
    }
    
    

    Automatic scalling done in g.DrawImage() can also make artefacts apear in the final picture (as do many other factors, e.g. AutoSize=true in an PictureBox). Depends on your code.

    P.S. Try setting g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit if you want to use TextRenderer.DrawText without those black ClearType outlines. At least on my current system (XP, SP3) this works fine.

    Marcel

    Thursday, July 22, 2010 5:42 PM
  • Try:

          using (Graphics g = Graphics.FromImage(result))
          {
            g.Clear(
    Color.Black);
            
    //do the fill stuff;
          }

    Thursday, July 22, 2010 10:18 PM
  • Hi John,

    Your suggestion works because after g.Clear() executes we have a black background and the outlines of the text drawn with TextRenderer.DrawText are also black. But wouldn't he loose his already loaded bitmap?

    Marcel

    Friday, July 23, 2010 5:07 AM
  • Your suggestion works because after g.Clear() executes we have a black background and the outlines of the text drawn with TextRenderer.DrawText are also black. But wouldn't he loose his already loaded bitmap?

    What already loaded bitmap?  I didn't think it woud be necessary to repeat all of the OP's code.

    result = new Bitmap(client.Width, client.Height);

     

    Friday, July 23, 2010 7:08 AM
  • Hi John,

    Oops. Sorry. It was me misunderstanding the OP's: "If the Graphics is delivered from a UserControl.OnPaint, it works. If it is taken from a BitMap [...]".

    Nevertheless: You're suggestion is only hiding the artefacts. It's curing the effects, not the cause. Just use g.Clear(Color.White) and TextRenderer.DrawText(w, x, y, z, Color.White). You'll see what I mean. Now add:

    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

    Isn't this better?

    Marcel

    Friday, July 23, 2010 7:40 AM
  • Hi Marcel and John,

     

    Thank you for your participation in this post.

    It helped me alot.

    The following line did it:

    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
    

    BR

    Peter

    Friday, July 23, 2010 2:09 PM