Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3303474
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:57:39+00:00 2026-05-17T20:57:39+00:00

I want to take a string, and a specified font, and create a graphic

  • 0

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();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:57:39+00:00Added an answer on May 17, 2026 at 8:57 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following Java string: Hello, World I want to take that string and
I want to write a program that would take a string, let's say Fox
I want to create some mad robust code. I want to take a query
I want to implement a String comparison function that doesn't take a different amount
I want to take HTML form data and handle the submited data(string for example)
I have a Dictionary<int, string> which I want to take the Key collection into
I want to take a math expression that takes variables and print (assign to
I want to take backup or restore of my oracle database through .Net code.
I have an existing application that I'm supposed to take and create a mini
Lets say I have List<string> = new List<string>() {20,26,32} I want to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.