Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does DrawString look so crappy?

I am trying to add a text scale to a color image. The agcScale.jpg image (below) is 2 winform labels on the top and bottom and 2 winform pictureboxes on the left and right. The exact same code was used to produce the strings in the right and left pictureboxes, the only difference is that pictureBoxAgcVscale contains only the strings. Why does DrawString in pictureBoxAgc look fine but DrawString in pictureBoxAgcVscale look so bad? I can probably fix pictureBoxAgcVscale by doing a bmp.SetPixel for each pixel but that seems like the wrong way to fix this.

agcScale.jpg

private void DisplayAgcVscale(double min, double max)
{
    var bmp = new Bitmap(pictureBoxAgcVscale.Width, pictureBoxAgcVscale.Height);
    var c = (max - min) / bmp.Height;
    using (var g = Graphics.FromImage(bmp))
    {
        var font = new Font("Microsoft Sans Serif", 8.25F);
        var y1 = bmp.Height / 10;
        for (var y = y1; y < bmp.Height; y += y1)
        {
            var agc = y * c + min;
            var text = agc.ToString("#0.000V");
            var h = bmp.Height - y - font.Height / 2;
            g.DrawString(text, font, Brushes.Black, 0, h);
        }
    }
    pictureBoxAgcVscale.Image = bmp;
}
like image 978
jacknad Avatar asked Oct 25 '11 16:10

jacknad


1 Answers

You are drawing black text on a transparent background. The anti-aliasing pixels are fading from black to black, no choice, turning the letters into blobs. It works for the text on the left because you draw the pixels first.

You forgot g.Clear().

like image 114
Hans Passant Avatar answered Nov 08 '22 18:11

Hans Passant