views:

544

answers:

2
A: 

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
If you omit `Bitmap`'s `PixelFormat`, it defaults to `Format32bppArgb` anyway.
Pavel Minaev
+3  A: 

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 ingores 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;
Pavel Minaev
This worked well, thank you! It isn't quite as pretty as ClearType, but even fonts designed for ClearType (ex. Consolas) still look fine.
Ian