3

Here's the OnPaint method of a control that simply inherits from control and provides a property to get/set the textrenderinghint:

 Private _mode as TextRenderingHint = SystemDefault.
 Public Property Mode as TextRenderingHint
    Get & Set _mode
 ...

 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    Dim g = e.Graphics
    Dim savMode = g.Save
    g.TextRenderingHint = Me._mode
    g.DrawString(Me.Text, Me.Font, Brushes.Black, 0, 0)
    g.Restore(savMode)
    MyBase.OnPaint(e)
 End Sub

Now, if you place two of these on a form, leave the first as default and change the second to AntiAlias, it looks fine at design-time but when you run the app, the first label's rendering mode has changed. Its as if the DrawString method has changed the systemdefault.

Here's some observations:

(1) If I explicity set the first control's mode to ClearTypeGridFit, which is the same as the default in my case, it fixes the problem.

(2) If you place a third control on the form and leave at the default mode, it fixes the problem.

(3) TextRenderer.DrawText doesn't replicate the problem.

(4) If I inherit label control and override the onpaint method to set the rendering mode, the problem is not replicated even though I set UseCompatibleTextRendering - which forces the label to render with DrawString instead of DrawText.

I'm on XP with cleartype enabled and using visual studio 2008 express edition.

ETA: I've tried it in C# and the same thing happens

Jules
  • 4,319
  • 3
  • 44
  • 72

3 Answers3

1

This sounds like the issue we ran into before. In your app startup code, is there a call to Application.SetCompatibleTextRenderingDefault(true)? (Or it might be set to false, I forget).

If so, toggle the state of that bool to change the text rendering mode and it should work as expected.

Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
  • Unfortunately there is no call. I'd be surprised if that did solve the issue, though, because I'm drawing the text myself and explicitly using the gdi+ drawstring method. Also, with my observation 2, above, it seems to be a deeper issue. – Jules Oct 01 '10 at 21:38
  • Ok. Might still be worth a shot, add that call to your app startup. Try with both SetCompatibleTextRenderingDefault(true) and SetCompatibleTextRenderingDefault(false) and see what effect it has on your application. It's been awhile, having long since switched to WPF, but I seem to recall this having an effect even on the DrawString stuff. – Judah Gabriel Himango Oct 01 '10 at 21:44
0

This bug still seems to be present in .Net Framework 4.8. I found out that it can be solved, when the first call to DrawString() since application start is done with SystemDefault-TextRenderingHint:

using (Bitmap bmp = new Bitmap(1, 1))
using (Graphics gfx = Graphics.FromImage(bmp))
{
    // gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;    // bug
    gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;   // no bug
    gfx.DrawString("x", SystemFonts.DefaultFont, Brushes.Black, 0, 0);
}

So if this is done once in

[STAThread]
static void Main(string[] args)
{ .. }

every following call to DrawString works correct with or without setting TextRenderingHint before.

user1027167
  • 4,320
  • 6
  • 33
  • 40
0

I had a similar problem. I called this:

Image i = new Bitmap(size, size);
Graphics g = Graphics.FromImage(i);

// When this line is uncommented TextRenderingHint is broken for ALL other Graphics-Objects.
// Setting "g.TextRenderingHint" later works sometimes in unpredictable ways.
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
...

My startup code looked like this:

[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

Then I tried what Judah Himango suggested and everything worked just fine.
This looks definitely like a bug to me!

So just make sure you call:

    Application.SetCompatibleTextRenderingDefault(true);

Worked for me!

Simon Ottenhaus
  • 715
  • 2
  • 8
  • 19