简体   繁体   中英

How to draw solid strings to a bitmap

The following code

var fnt = new Font("Consolas", 12, FontStyle.Regular);
var bmp = new Bitmap(24, 24);

using (var grp = Graphics.FromImage(bmp))
{
    var size = grp.MeasureString(shortName, fnt, new SizeF(24, 24),
         new StringFormat(StringFormatFlags.NoFontFallback | StringFormatFlags.NoWrap |
                   StringFormatFlags.FitBlackBox));
    grp.TranslateTransform(0, (bmp.Height - size.Height) / 2);
    grp.DrawString("DW", fnt, Color.FromArgb(0xff, 0x33, 0x66, 0x33), 
         new RectangleF(0, 0, 24, 24),
        new StringFormat(StringFormatFlags.NoFontFallback | 
            StringFormatFlags.NoWrap | StringFormatFlags.FitBlackBox));
    grp.TranslateTransform(0, (size.Height - bmp.Height) / 2);
}
bmp.Save(string.Format("d:\\out-{0}.bmp", shortName));

gives output looks like , which does not look good (when magnified you can see black spots in border). If change the brush above to Brushes.Black we get which is better but it is too bold (note I set the font style to "regular").

However I can achieve something like manually, which is clear and thin. That one was created using GIMP, with the same font family, font size, and color on a transparent canvas.

So I wonder how I can do the same thing with GDI+ programatically?

duplicate?? Font is very ugly

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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