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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:08:37+00:00 2026-05-12T17:08:37+00:00

Given a number of pixels (say: 300) and a Font (fixed type, fixed size,

  • 0

Given a number of pixels (say: 300) and a Font (fixed type, fixed size, like Consolas), how could I determine the maximum number of characters that I could draw with GDI+?

The text won’t be written to a Label or such, but drawn using GDI+:

public void DrawString( string s, Font font, Brush brush, 
    RectangleF layoutRectangle, StringFormat format );

But I want to perform certain text operations before drawing, hence why I’d like to figure out the maximum number of chars I can safely output.

  • 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-12T17:08:38+00:00Added an answer on May 12, 2026 at 5:08 pm

    The System.Drawing.Graphics method MeasureString pads string widths with a few extra pixels (I do not know why), so measuring the width of one fixed-length character and then dividing that into the maximum width available would give a too-low estimate of the number of characters that could fit into the maximum width.

    To get the maximum number of characters, you’d have to do something iterative like this:

    using (Graphics g = this.CreateGraphics())
    {
        string s = "";
        SizeF size;
        int numberOfCharacters = 0;
        float maxWidth = 50;
        while (true)
        {
            s += "a";
            size = g.MeasureString(s, this.Font);
            if (size.Width > maxWidth)
            {
                break;
            }
            numberOfCharacters++;
        }
        // numberOfCharacters will now contain your max string length
    }
    

    Update: you learn something new every day. Graphics.MeasureString (and TextRenderer) pad the bounds with a few extra pixels to accomodate overhanging glyphs. Makes sense, but it can be annoying. See:

    http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx

    Looks like a better way to do this is:

    using (Graphics g = this.CreateGraphics())
    {
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        SizeF size = g.MeasureString("a", this.Font, new PointF(0, 0), 
            StringFormat.GenericTypographic);
        float maxWidth = 50; // or whatever
        int numberOfCharacters = (int)(maxWidth / size.Width);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given any number, what's the best way to determine it is even? how many
The task is to generate a given number of numeric pins of a given
How can I rotate an Bitmap a given number of degrees while maintaining the
How can I find the least prime number greater than a given number? For
Can anyone tell me how to format a given number of seconds (for example
This question asks how to compute the Cartesian product of a given number of
I have a requirement for the generation of a given number N of vectors
There are more than one solution for finding the-number-of-digits in a given number. For
I am trying to find the fastest way to check whether a given number
I am looking for a sound algorithm that would randomly place a given number

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.