Question

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;
}
Was it helpful?

Solution

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().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top