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 784435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:42:49+00:00 2026-05-14T20:42:49+00:00

I have the following .ashx page that takes some query string parameters and returns

  • 0

I have the following .ashx page that takes some query string parameters and returns a bitmap with the specified text written on it.
The problem I have is that I am currently just manually setting the initial size of the bitmap at 100 X 100 when what I really want is to have the bitmap be just big enough to include all the text that was written to it.
How can I do this?

  public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "image/png";
      string text = context.Request.QueryString["Text"];
    //set FontName
    string fontName;
    if (context.Request.QueryString["FontName"] != null)
    {
        fontName = context.Request.QueryString["FontName"];
    }
    else
    {
        fontName = "Arial";
    }
    //Set FontSize
    int fontEms;
    if (context.Request.QueryString["FontSize"] != null)
    {
        string fontSize = context.Request.QueryString["FontSize"];
        fontEms = Int32.Parse(fontSize);

    }
    else
    {
        fontEms = 12;
    }

    //Set Font Color
    System.Drawing.Color color;
    if (context.Request.QueryString["FontColor"] != null)
    {
        string fontColor = context.Request.QueryString["FontColor"];
        color = System.Drawing.ColorTranslator.FromHtml(fontColor);
        context.Response.Write(color.ToString());
    }
    else
    {
        color = System.Drawing.Color.Red;
    }
    using (System.Drawing.Text.PrivateFontCollection fnts = new System.Drawing.Text.PrivateFontCollection())
    using (System.Drawing.FontFamily fntfam = new System.Drawing.FontFamily(fontName))
    using (System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(color))
    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100))
    {
        using (System.Drawing.Font fnt = new System.Drawing.Font(fntfam, fontEms))
        {
            fnts.AddFontFile(System.IO.Path.Combine(@"C:\Development\Fonts\", fontName));
            System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
            graph.DrawString(text, fnt, brush, new System.Drawing.Point(0, 0));
            string imgPath = System.IO.Path.Combine(@"C:\Development\MyPath\Images\Text", System.IO.Path.GetRandomFileName());
            bmp.Save(imgPath);
            context.Response.WriteFile(imgPath);
        }
    }
}

Update:
I wound up creating the Bitmap larger than I would ever need, get the graphics from that bitmap and use the MeasureString method to get the size the text I am going to write and then crop that part of the bitmap into a new bitmap like so.

 using (System.Drawing.Text.PrivateFontCollection fnts = new System.Drawing.Text.PrivateFontCollection())
    using (System.Drawing.FontFamily fntfam = new System.Drawing.FontFamily(fontName))
    using (System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(color))
    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(500, 500))
    {
        using (System.Drawing.Font fnt = new System.Drawing.Font(fntfam, fontEms))
        {
            fnts.AddFontFile(System.IO.Path.Combine(@"C:\Development\Fonts\", fontName));
            System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
            System.Drawing.SizeF bmpSize = graph.MeasureString(text, fnt);
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), bmpSize.ToSize());
            System.Drawing.Bitmap croppedBmp = bmp.Clone(rect, bmp.PixelFormat);
            graph = System.Drawing.Graphics.FromImage(croppedBmp);
            graph.DrawString(text, fnt, brush, new System.Drawing.Point(0, 0));
            string imgPath = System.IO.Path.Combine(@"C:\Development\MyPath\Images\Text", System.IO.Path.GetRandomFileName());
            croppedBmp.Save(imgPath);
            context.Response.WriteFile(imgPath);
        }
    }
  • 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-14T20:42:49+00:00Added an answer on May 14, 2026 at 8:42 pm

    I would guess there’s a System.Drawing.Graphics.MeasureString method which will return the size of the specified text using the specified font.

    Then, you may resize your image accordingly, or resize your text accordingly in order to make them both fit together.

    Perhaps an eye out to this question and answers will guide you somehow, as the question is about sizing text so that it may fit within an area. You seem to wish to make something alike, so the algorithm may inspire you.

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

Sidebar

Related Questions

No related questions found

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.