开发者

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

开发者 https://www.devze.com 2023-04-05 20:33 出处:网络
I\'m using following C# code to make a picture with a text in it // Create font. Parameter is a global variable

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.Vertica开发者_JAVA百科l);

        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 :

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

(source: free.in.th)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号