Earth Engine
Earth Engine

Reputation: 10476

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?

Upvotes: 0

Views: 67

Answers (1)

wtfidgafmfgtfooh
wtfidgafmfgtfooh

Reputation: 46

duplicate?? Font is very ugly

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

Upvotes: 1

Related Questions