Question

I have the following code, which takes a string and adds it to a Bitmap in memory, which in turn is saved as a BMP file. The code I have at the moment is as follows;

string sFileData = "Hello World";
string sFileName = "Bitmap.bmp";

Bitmap oBitmap = new Bitmap(1,1);
Font oFont = new Font("Arial", 11, FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
int iWidth = 0;
int iHeight = 0;

using (Graphics oGraphics = Graphics.FromImage(oBitmap))
{
    oGraphics.Clear(Color.White);

    iWidth = (int)oGraphics.MeasureString(sFileData, oFont).Width;
    iHeight = (int)oGraphics.MeasureString(sFileData, oFont).Height;
    oBitmap = new Bitmap(oBitmap, new Size(iWidth, iHeight));

    oGraphics.DrawString(sFileData, oFont, new SolidBrush(System.Drawing.Color.Black), 0, 0);

    oGraphics.Flush();

}

oBitmap.Save(sFileName, System.Drawing.Imaging.ImageFormat.Bmp);

The problem I have is when I view the BMP file in Paint, the size of the bitmap is defined correctly, the background is white, however their is no text ?

What am I doing wrong ?

Was it helpful?

Solution

You are creating a Bitmap object and then binding a Graphics object to it in the using statement. However, you then destroy that Bitmap object and create a new one which loses that original binding. Try creating your Bitmap only once.

EDIT

I see that you're trying to use the Graphics object for two purposes, one to measure things and one to draw with. This isn't a bad thing but is causing your problems. I'd recommend reading the threads in this post for an alternative way for measuring strings. I'm going to use the helper class from this specific answer which I personally like the most.

public static class GraphicsHelper {
    public static SizeF MeasureString(string s, Font font) {
        SizeF result;
        using (var image = new Bitmap(1, 1)) {
            using (var g = Graphics.FromImage(image)) {
                result = g.MeasureString(s, font);
            }
        }
     return result;
    }
}

string sFileData = "Hello World";
string sFileName = "Bitmap.bmp";

Font oFont = new Font("Arial", 11, FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
var sz = GraphicsHelper.MeasureString(sFileData, oFont);

var oBitmap = new Bitmap((int)sz.Width, (int)sz.Height);

using (Graphics oGraphics = Graphics.FromImage(oBitmap)) {
    oGraphics.Clear(Color.White);
    oGraphics.DrawString(sFileData, oFont, new SolidBrush(System.Drawing.Color.Black), 0, 0);
    oGraphics.Flush();

}

oBitmap.Save(sFileName, System.Drawing.Imaging.ImageFormat.Bmp);

OTHER TIPS

It appears that you're swapping bitmaps midstream. You seem to be doing the following:

  1. Create a bitmap
  2. Get a graphics handle to the bitmap
  3. create a new bitmap with a different size

The problem is that you're still using the graphics handle (from step 2) which is associated with the old (first) bitmap, not the new (second) bitmap.

You need to use a graphics handle associated with the new (second) bitmap, not the old (first) bitmap.

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