Maciej Stachowski
Maciej Stachowski

Reputation: 1728

DrawString() seems not to work

 private static Bitmap[] renders = new Bitmap[characters];
    public static void initBitmaps()
    {
        fontWidth = TextRenderer.MeasureText("c", font).Width;
        fontHeight = TextRenderer.MeasureText("c", font).Height;
        for (int i=0; i<characters; i++)
        {
            renders[i] = new Bitmap(fontWidth, fontHeight);
            using (Graphics g = Graphics.FromImage(renders[i]))
            {
                g.DrawString(Convert.ToChar(i + 32).ToString(), font, new SolidBrush(Color.Black), new PointF(0, 0));
            }
        }
    }

After executing this bit of code, all bitmaps are empty (RawData are null). What am I doing wrong?

(the font in question is fixed-width, so size shouldn't be a problem)

Upvotes: 1

Views: 1066

Answers (1)

ventiseis
ventiseis

Reputation: 3099

DrawString works fine and the bitmaps aren't empty, you just can't see the text because you are drawing with a black brush on a black background.

You'll need to initialize the bitmap; use g.Clear(Color.White). Also note that you are mixing TextRenderer with Graphics.DrawString, which is a bad idea. See DrawString vs. TextRenderer for more information.

If you try proportional fonts, you are going to be disappointed how W and M will fit because you're only measuring the dimensions of lower case c which (in most fonts) would be smaller than a upper case W.

Upvotes: 1

Related Questions