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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:58:12+00:00 2026-06-14T02:58:12+00:00

Say I have the string Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus

  • 0

Say I have the string “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus arcu massa, tempus non tincidunt ut, tempus sit amet odio. Mauris in dui sed enim vulputate dictum.”

I want that string wrapped by a specific length (in pixels) and without breaking words. 80 pixels for example.

I have the Font variable that is used to draw the string and the Graphics variable (often called “g”) so I can measure the length of the string where needed.

All samples that I found only wrap text by character-length but I need it in pixels for GDI+ drawing.
I do not want to use the TextRenderer control because it seems to bug. Sometimes it measures it’s own text-height wrong. It’s rare but it happens.

So far I got the following:

public static string WrapTextByPixels(string text, ref Graphics g, Font font, float maxWidth)
        {
            string[] originalLines = text.Split(new[] { " " }, StringSplitOptions.None);

            var wrapBuilder = new StringBuilder();

            float currentLineWidth = 0;

            foreach (var item in originalLines)
            {
                float itemWidth = g.MeasureString(item, font).Width;

                currentLineWidth += itemWidth;
                if (currentLineWidth > maxWidth ||
                    itemWidth > maxWidth) // When a single word is longer than the maxWidth then just add it
                {
                    wrapBuilder.Append(Environment.NewLine);
                    currentLineWidth = 0;
                }
                wrapBuilder.Append(item + " ");
            }

            return wrapBuilder.ToString();
        }

But the above code doesn’t work. Some lines are still too long.

  • 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-14T02:58:13+00:00Added an answer on June 14, 2026 at 2:58 am

    You cannot bypass TextRenderer here, you must use its MeasureText() method so the calculated layout matches what is rendered, later, when the DrawText() method renders the text. Other ways to calculate string length, like Graphics.MeasureString() will just produce more error, the text layout for the Graphics class is very different. And pretty broken, the reason that TextRenderer was added in .NET 2.0

    If you get bad results then that’s almost always because you didn’t specify the TextFormatFlags correctly. It must exactly match the flags that will be used in the DrawText() method call. That’s not always easy to find out, especially when the text is drawn by painting code inside the framework. Use the Reference Source to find out.

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

Sidebar

Related Questions

If I have a long string, lets say var str = Lorem ipsum dolor
Say I have the following my strings.xml file: <string name=string0>Lorem</string> <string name=string1>ipsum</string> <string name=string2>dolor</string>
Let's say I have a string like so: Lorem ipsum de color [post]57[/post] sit
let's say i have a string like this: $string = 'Lorem Ipsum available, <a
Lets say you have an HTML string like this: <div id=loco class=hey >lorem ipsum
Let's say I have a string that I retrieve from a DB like: Lorem
Let's say i have this string: $string = lorem, lpsum, is, simply, dummy, text,
Lets say a have a string such as this one: string txt = Lore
Say I have the string s = Controller = require 'controller' class foo view:
Say you have a string 3.4564. A sample set in the database has these

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.