为什么System.Drawing中+ ClearType字体有丑陋的黑色碎片? [英] Why System.Drawing + ClearType font have ugly black fragments?

查看:927
本文介绍了为什么System.Drawing中+ ClearType字体有丑陋的黑色碎片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的C#代码,以使在它的文本

  //创建字体的图片。参数是一个全局变量
字体objFont =新的字体(字体名称,字号,fontstyle的,System.Drawing.GraphicsUnit.Pixel);

//抓住从图片框现有的图像。 (目标是图片框的名字)
位图的结果;
如果(target.Image!= NULL)
{
结果=新位图(target.Image);
}
,否则
{
结果=新位图(target.Width,target.Height);
}
图形objGraphics = Graphics.FromImage(结果);

//并将其绘制到它。选择带有复选框的模式。

objGraphics.SmoothingMode = SmoothingMode.HighQuality;
如果(checkBox1.Checked!)
{
objGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
}
,否则
{
objGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
}
刷B =新一个LinearGradientBrush(新的Rectangle(新点(X,Y),objGraphics.MeasureString(文字,objFont).ToSize()),颜色1,颜色2,LinearGradientMode.Vertical);

objGraphics.DrawString(文字,objFont,B,X,Y);
objGraphics.Save();

//设置结果PictureBox的

target.Image =结果;

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

在此之前的代码,target.BackColor已设置到像

$所需的颜色b
$ b

  target.BackColor = Color.Black; 



<:

这是结果IMG SRC =http://image.free.in.th/z/ie/yqbsg.png>



我想知道这是什么原因ClearType字体看起来这么难看明亮的BG? (在BG像暗紫色,你不会注意到黑色边框,但它仍然存在)


解决方案

 别的
{
结果=新位图(target.Width,target.Height);
}

这是一个问题,你有没有初始化的位图的像素。他们会默认为Color.Transparent。这将导致文本以消除锯齿为黑色,因为Color.Transparent有红,绿,蓝为0时则显示对一个粉红色的背景位图,反锯齿像素变得非常明显,因为他们并没有吸引到融合成粉红色的背景。他们只是好看在黑色背景上。



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


I'm using following C# code to make a picture with a text in it

            // 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();

prior to this code, target.BackColor has been set into a desired color like

target.BackColor = Color.Black;

This is the results :

I was wondering that why ClearType font looks so ugly on bright bg? (On bg like dark purple you won't notice black border but it's still there)

解决方案

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

That's a problem, you haven't initialized the pixels of the bitmap. They'll default to Color.Transparent. Which causes text to be anti-aliased to black since Color.Transparent has red, green and blue at 0. When you then display the bitmap against a pink background, the anti-aliasing pixels become very visible since they weren't drawn to blend into a pink background. They only look good on a black background.

You'll need to use Graphics.Clear(). Or give up on anti-aliasing if the transparency was intended.

这篇关于为什么System.Drawing中+ ClearType字体有丑陋的黑色碎片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆