9

I'd like to draw antialiased text on a transparent bitmap and have the antialiasing drawn as alpha blended pixels. This way, I can draw the bitmap onto any color surface (or an image, for that matter) and the antialiasing still looks fine.

Here is a simplified sample showing the problem:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Bitmap bitmap = new Bitmap(this.Width, this.Height);
    Graphics g = Graphics.FromImage(bitmap);
    g.Clear(Color.Empty);
    g.DrawString("hello world", new Font(this.Font.FontFamily, 24), Brushes.Blue, new Point(50, 50));
    e.Graphics.DrawImage(bitmap, new Point(0, 0));
}

And here is the result:

alt text

The ultimate goal of this is to use UpdateLayeredWindow to draw my transparent alpha blended window. I am creating a Conky-like application, and I'd like to be able to use ClearType rendering for text (this is easy without antialiasing, of course).

Currently, I grab the screen behind the form, draw that, and then draw my text. It looks good, but has to be updated and is slow to draw. Any other ideas to accomplish drawing text on the desktop would also be welcome.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
iano
  • 2,061
  • 1
  • 18
  • 22

2 Answers2

16

Your text is displayed as it is because you have ClearType subpixel anti-aliasing mode enabled (which is the default on Vista and above). ClearType, by definintion, cannot play nice with alpha channel, since it blends colors, and thus isn't background-agnostic. So it ignores the alpha channel, and blends to black (which is your transparent color otherwise is). You need to enable plain grayscale anti-aliasing for the desired effect:

g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
  • This worked well, thank you! It isn't quite as pretty as ClearType, but even fonts designed for ClearType (ex. Consolas) still look fine. – iano Aug 18 '09 at 15:09
  • Curiously, for me `AntiAliasGridFit` produces text with zero anti-aliasing. `AntiAlias`, however, works. – Roman Starkov Dec 26 '11 at 01:21
  • `AntiAliasGridFit` might not do anti-aliasing if the font size is too small. At such font sizes, `AntiAlias` gives an ugly result anyway. – Francis Gagné Jul 08 '13 at 22:31
3

I'm not sure if it's really necessary, but if you want to do alpha-blending, you should specify a 32-bit image:

Bitmap bitmap = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

Using your example, I was able to make the text look decent by adding a text rendering hint:

g.Clear(Color.Empty);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.DrawString("hello world", new Font(this.Font.FontFamily, 24), Brushes.Blue, new Point(50, 50));

Is this doing what you want, or just hiding the problem?

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94