1
 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)

Maciej Stachowski
  • 1,708
  • 10
  • 19
  • 1
    this kind of problem can solve quickly using breakpoints.does it come into your using block? – Arash Aug 24 '13 at 19:45
  • 7
    It isn't empty, you just can't see it. You are drawing with a black brush on a black background. You'll need to at least initialize the bitmap, use g.Clear(Color.White). Also note that you are mixing TextRenderer with Graphics, bad idea. And you are going to be disappointed how W and M will fit. – Hans Passant Aug 24 '13 at 19:47
  • 1
    @Hans Passant - thanks, this has solved the problem :) – Maciej Stachowski Aug 24 '13 at 19:51

1 Answers1

1

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.

Community
  • 1
  • 1
ventiseis
  • 3,029
  • 11
  • 32
  • 49