问题描述
我想绘制透明位图抗锯齿文本并且绘制为alpha混合像素的抗锯齿。这样一来,我可以绘制位图上任何颜色的表面(或图像,为此事)和反锯齿看起来仍然不错。
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));
}
这是结果:
这样做的最终目的是使用UpdateLayeredWindow来绘制我透明的alpha混合窗口。我创建的Conky般的应用程序,我希望能够使用的ClearType渲染文本(这是很容易没有抗锯齿,当然)。
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.
推荐答案
显示您的文字,因为它是因为你必须启用ClearType的子像素反锯齿模式(这是在Vista及以上默认值)。 ClearType的,通过definintion,不能发挥alpha通道不错,因为它融合色彩,因而并不背景无关。因此,它忽略了Alpha通道,和混合黑色(这是你的透明色,否则是)。您需要启用纯灰度抗锯齿预期的效果:
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;
这篇关于透明位图抗锯齿文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!