繁体   English   中英

为什么System.Drawing + ClearType字体有难看的黑色片段?

[英]Why System.Drawing + ClearType font have ugly black fragments?

我正在使用以下C#代码制作带有文字的图片

            // Create font. Parameter is a global variable
        Font objFont = new Font(fontname, fontsize, fontstyle, System.Drawing.GraphicsUnit.Pixel);

        // Grab an existing image from picture box. (target is picturebox's name)
        Bitmap result;
        if (target.Image != null)
        {
            result = new Bitmap(target.Image);
        }
        else
        {
            result = new Bitmap(target.Width, target.Height);
        }
        Graphics objGraphics = Graphics.FromImage(result);

        // And draw to it. Select a mode with check box.

        objGraphics.SmoothingMode = SmoothingMode.HighQuality;
        if (!checkBox1.Checked)
        {
            objGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
        }
        else
        {
            objGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
        }
        Brush b = new LinearGradientBrush(new Rectangle(new Point(x, y), objGraphics.MeasureString(text, objFont).ToSize()),color1,color2,LinearGradientMode.Vertical);

        objGraphics.DrawString(text, objFont, b, x, y);
        objGraphics.Save();

        //Set the result to picturebox

        target.Image = result;

        objGraphics.Dispose();
        b.Dispose();

在此代码之前,target.BackColor已设置为所需的颜色,例如

target.BackColor = Color.Black;

结果如下:


(来源: free.in.th

我想知道为什么ClearType字体在明亮的bg上看起来如此丑陋? (在类似深紫色的bg上,您不会注意到黑色边框,但是它仍然存在)

    else
    {
        result = new Bitmap(target.Width, target.Height);
    }

这是一个问题,您尚未初始化位图的像素。 它们将默认为Color.Transparent。 由于Color.Transparent在0处具有红色,绿色和蓝色,这会导致文本被抗锯齿化为黑色。当您在粉红色背景上显示位图时,抗锯齿像素变得非常可见,因为它们没有被绘制为融合变成粉红色的背景。 它们仅在黑色背景上看起来不错。

您需要使用Graphics.Clear()。 或者,如果要透明,则放弃抗锯齿。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM