Why does DrawString look so crappy?
Asked Answered
C

2

11

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;
}
Cincinnatus answered 25/10, 2011 at 16:10 Comment(6)
Not the most professional title to a question, eh?Susurration
Can you edit this into a short but complete program? I'll give it a go myself, but if you could do it it would be simpler...Tantalizing
@JonSkeet: I narrowed it down.Fadein
Have you tried using anti aliasing?Lubra
@JoshuaEvensen I'd guess it's anti aliasing (together with not clearing the background) than causes this problem.Solatium
Hi jacknad. This is very relevant to what I am doing. I was wondering if this is open-sourced so I could take a look at how you make this graph legend?Kidwell
B
15

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

Barrack answered 25/10, 2011 at 16:26 Comment(3)
g.Clear on a blank image? Have to try this now.Fadein
Thanks a million. g.Clear() works fine, but it was counter intuitive (to me) that g.Clear() is needed for a transparent background.Cincinnatus
Thanks..I resolve bad text quality with this idea of g.Clear().. I had to write text directly on image. g.Clear() helped me. In my case I could not use g.clear() directly on image loaded in Graphics object. First I create new empty bitmap, then I use g.Clear(), then I write text on this new bitmap and at the end I overlay this bitmap on original image.Cantonment
T
0

I had a similar issue, but in a listbox, and it wasn't resolved by clearing the rectangle. I had to apply a "TextRenderingHint":

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
e.Graphics.DrawString(listText, myFont, myBrush, e.Bounds, StringFormat.GenericDefault);
Tempera answered 11/5, 2022 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.