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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:05:39+00:00 2026-06-09T04:05:39+00:00

I developing a program like Word. For that I need to write strings into

  • 0

I developing a program like Word. For that I need to write strings into the same “line”, and I also want the ability to format each word/letter/sentence differently, controlling color, size, font, etc.

So I have to calculate the pixel length and height of a string, to put them into a line. But there is a problem with the function (Graphics) MeasureString, this function puts extra length infront/behind a string. This function has also has some problems with white space.

I tried a different StringFormater, but everytime they return too much length or not enough.

Does a function exist to write different formated strings on the Graphics object or a function to calculate the exact pixel length of a string?

  • 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-06-09T04:05:40+00:00Added an answer on June 9, 2026 at 4:05 am

    First off, this is a fairly tricky problem to solve properly – you may be better off trying to find an existing control (e.g. the RichTextBox) that solves this for you.

    That said if you do want to do this then this is more-or-less the correct way to solve this problem, however if you take a look at the MeasureString documentation you will note that the behaviour you are seeing is intentional

    The MeasureString method is designed for use with individual strings
    and includes a small amount of extra space before and after the string
    to allow for overhanging glyphs. Also, the DrawString method adjusts
    glyph points to optimize display quality and might display a string
    narrower than reported by MeasureString. To obtain metrics suitable
    for adjacent strings in layout (for example, when implementing
    formatted text), use the MeasureCharacterRanges method or one of the
    MeasureString methods that takes a StringFormat, and pass
    GenericTypographic. Also, ensure the TextRenderingHint for the
    Graphics is AntiAlias.

    So it sounds to me like you should instead be using the Graphics.MeasureCharacterRanges Method instead.

    Here is a sample I prepared that deals renders some text in two different colours. To try it out just paste it into a new form

    protected override void OnPaint(PaintEventArgs e)
    {
        // This is where we wish to print our string
        var region = new RectangleF(50, 50, 200, 50);
    
        // This is the font we wish to use
        var font = new Font("Times New Roman", 16.0F);
    
        // Draw a string for comparison
        DrawString(e.Graphics, "RedBlack", font, Brushes.Black, new RectangleF(50, 150, 200, 50));
    
        // Draw the first string and keep a track of the Region it was rendered in
        var first = DrawString(e.Graphics, "Red", font, Brushes.Red, region);
    
        // Adjust the region we wish to print 
        region = new RectangleF(region.X + first.GetBounds(e.Graphics).Width, region.Y, region.Width, region.Height);
    
        // Draw the second string
        DrawString(e.Graphics, "Black", font, Brushes.Black, region);
    
        base.OnPaint(e);
    }
    
    private Region DrawString(Graphics g, string s, Font font, Brush brush, RectangleF layoutRectangle)
    {
        var format = new StringFormat();
        format.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, s.Length) });
    
        g.DrawString(s, font, brush, layoutRectangle, format);
        return g.MeasureCharacterRanges(s, font, layoutRectangle, format)[0];
    }
    

    This is what it looks like

    screenshot of form

    Note that you need to be careful with clipping – GDI will "wrap" rendered text onto new lines for you by default, however this wont work any more, you will end up with something like this

    screenshot of form with incorrect line wrapping

    Also if you try and print out text with different fonts / font sizes then the "bottom" of each of those fonts won’t line up where you expect it to. Try taking a look at Formatting text on a common baseline for some hints on how to deal with that.

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

Sidebar

Related Questions

I am developing a program that need read/write some files from sdcard or internal
I'm developing a program that would require huge amount of memory, and I want
I'm developing memory management program, that the Form1_Load Sub is like this: Private Sub
I'd like to write s simple Haskell program that connects to a SOAP service.
I have a WPF program that I'm developing in which I want to catch
I'm developing a program that needs to parse the file name into a TTextField
I'm developing a program in windows (something like client & server) and I want
im developing a program in which I need to make a save file that
I am developing a program where I find myself doing this like this a
So suppose I'm developing a chess-like program using Java's Swing. I added a MouseListener

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.