views:

25

answers:

1

I want to take a string, and a specified font, and create a graphic image that contains that text in box, where the box resizes to the text.

I got the code below working, but it sizes the box before doing anything else. Is there some way to resize the image to fit the text (I probably need to add a small border of a few pixels).

Also, I wasn't really sure why I need to pass the x,y coordinates if I want the string centered.

  //Sample calls to function below 
        renderImage("Hello World", "Arial", 12, "test12.png");
        renderImage("Hello", "Arial", 16, "test16.png");
        renderImage("Peace Out", "Arial", 24, "test24.png");


static void renderImage(string text, string fontName, string filename, int fontsize) 
{

    {
        System.Drawing.Bitmap objBMP = null;
        System.Drawing.Graphics objGraphics = null;
        System.Drawing.Font objFont = null;

        // Create new image - bitmap
        objBMP = new Bitmap(531, 90);

        // Create a graphics object to work with from the BMP
        objGraphics = System.Drawing.Graphics.FromImage(objBMP);

        // Fill the image with background color
        objGraphics.Clear(Color.Yellow);

        // Set anti-aliasing for text to make it better looking
        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

        // Configure font to use for text
        objFont = new Font(fontName, fontsize, FontStyle.Bold);

        // Try StringFormat 
        StringFormat objStringFormat = new StringFormat();
        objStringFormat.Alignment = StringAlignment.Center;
        objStringFormat.LineAlignment = StringAlignment.Center;


        // Write out the text
        objGraphics.DrawString(text, objFont, Brushes.Black, 1, 1, objStringFormat);


  // Set the content type and return the image
        objBMP.Save(filename, ImageFormat.Png);

    }

UPDATE:

Got it working based on answer, but is there a more efficient way:

static void Main(string[] args)
{
    renderImage("Hello World", "Arial", 12, "test12.png");
    renderImage("Hello", "Arial", 16, "test16.png");
    renderImage("Peace Out", "Arial", 24, "test24.png");
}

static void renderImage(string text, string fontName, int fontSize, string filename) 
{

    {
        System.Drawing.Bitmap objBMP = null;
        System.Drawing.Graphics objGraphics = null;
        System.Drawing.Bitmap objBMPTemp = null;
        System.Drawing.Graphics objGraphicsTemp = null;
        System.Drawing.Font objFont = null;

        // Configure font to use for text
        objFont = new Font(fontName, fontSize, FontStyle.Bold);

        // Create new image - bitmap
        SizeF objSizeF = new SizeF();
        objBMPTemp = new Bitmap(100, 100); // some temp dummy size 

        // Create a graphics object to work with from the BMP
        objGraphicsTemp = System.Drawing.Graphics.FromImage(objBMPTemp);

        objSizeF = objGraphicsTemp.MeasureString(text, objFont);
        objBMP = new Bitmap((int)objSizeF.Width, (int)objSizeF.Height); 
        objGraphics = System.Drawing.Graphics.FromImage(objBMP); 



        // Fill the image with background color
        objGraphics.Clear(Color.Yellow);

        // Set anti-aliasing for text to make it better looking
        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;


        // Try StringFormat 
        //StringFormat objStringFormat = new StringFormat();
        //objStringFormat.Alignment = StringAlignment.Center;
        //objStringFormat.LineAlignment = StringAlignment.Center;
        //objStringFormat.FormatFlags



        // Write out the text
        //objGraphics.DrawRectangle(new Pen(Color.Cyan, 1), 0.0F, 0.0F, objSizeF.Width, objSizeF.Height);
        objGraphics.DrawString(text, objFont, Brushes.Black, 1, 1);
        //objGraphics.DrawString(text, objFont, Brushes.Black, 



        // Set the content type and return the image
        objBMP.Save(filename, ImageFormat.Png);


        // Kill our objects
        objFont.Dispose();
        objGraphics.Dispose();
        objBMP.Dispose();
    }

}
+2  A: 

You can use Graphics.MeasureString to measure the size of a string given a font and a size.

klausbyskov
+1 Have used Graphics.MeasureString and it sounds like what you want
Neil Fenwick
How do I get around the chicken and egg problem. I have to have an image object before I instantiate Graphics object, and I have to have a graphics object before I instantiate MeasureString. Do I need two graphics objects?
NealWalters
Got it working, but it seems convoluted. See update in original question. Also, why does SizeF give floats and creating image want integers?
NealWalters