Search code examples
c#imagegdi+drawstring

The text is too bold by DrawString on C#


I have use GDI DrawString method to draw text. When the program is running, the text on the screen seems very good, however once I saved the files to image, the font will be bolder than before. The normal will be bold, the bold will be much bolder. How to deal with this?

public override void DrawTo(Graphics g, int x, int y, int flag)
    {
        if (flag == 1)
        {
            Pen dpen = new Pen(Color.FromArgb(128, 0, 0, 0), 1);
            dpen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
            g.DrawRectangle(dpen, new Rectangle(Bounds.X + x, Bounds.Y + y, Bounds.Width, Bounds.Height));
        }

        if (!string.IsNullOrEmpty(Text))
        {

            g.DrawString(Text, Font, new SolidBrush(Color), new Rectangle(Bounds.X + x, Bounds.Y + y, Bounds.Width, Bounds.Height));
        }
    }

Solution

  • I got the solution.

    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    

    Just AntiAlias will solve that.