See how much text fits in a rectangle in C#

[text fits]

This program determines how much text fits in the left PictureBox and draws it. It then draws the remaining text in the right PictureBox. If you change the form’s size, the size of the PictureBox controls change so how much text fits in the left PictureBox also changes.

The question is, “How does the program figure out how much text fits in the left PictureBox?” The answer is to use the Graphics object’s MeasureString method.

The following code shows how the program draws the text in the two PictureBox controls. It looks complicated because there are a lot of details involving creating Bitmap objects, making associated Graphics objects, creating StringFormat objects, and so forth. However, all of that is relatively straightforward. The most interesting part, which determines how much text fits in the left PictureBox, is shown in blue.

// Draw as much text as possible in picText1.
// Draw the remainder in picText2.
private void DrawText()
    string text = "    This is a fairly long piece of text ...";

    // Make a font for printing.
    using (Font font = new Font("Times New Roman", 12))
        // Make a StringFormat to align text normally.
        using (StringFormat string_format = new StringFormat())
            // Stop drawing at a word boundary.
            string_format.Trimming = StringTrimming.Word;

            // Make a Bitmap for picText1.
            Bitmap bm1 = new Bitmap(
            using (Graphics gr = Graphics.FromImage(bm1))

                // Make a Rectangle representing where
                // the text should be drawn.
                const int margin = 5;
                Rectangle rect = new Rectangle(margin, margin,
                    picText1.ClientSize.Width - 2 * margin,
                    picText1.ClientSize.Height - 2 * margin);
                gr.DrawRectangle(Pens.LightGreen, rect);

                // See how much of the text will fit in picText1.
                int chars_fitted, lines_filled;
                SizeF avail_size =
                    new SizeF(rect.Width, rect.Height);
                gr.MeasureString(text, font,
                    avail_size, string_format,
                    out chars_fitted, out lines_filled);

                // Draw the text that will fit.
                string text_that_fits =
                    text.Substring(0, chars_fitted);
                gr.DrawString(text_that_fits, font,
                    Brushes.Black, rect, string_format);

                // Display the result.
                picText1.Image = bm1;

                // Remove the printed text from the total text.
                text = text.Substring(chars_fitted);
            } // End drawing on picText1

            // Draw the remaining text on picText2.
            // Make a Bitmap for picText2.
            Bitmap bm2 = new Bitmap(
            using (Graphics gr = Graphics.FromImage(bm2))

                // Make a Rectangle representing where
                // the text should be drawn.
                const int margin = 5;
                Rectangle rect = new Rectangle(margin, margin,
                    picText2.ClientSize.Width - 2 * margin,
                    picText2.ClientSize.Height - 2 * margin);
                gr.DrawRectangle(Pens.LightGreen, rect);

                // Draw the text.
                gr.DrawString(text, font,
                    Brushes.Black, rect, string_format);
            } // End drawing on picText2

            // Display the result.
            picText2.Image = bm2;
        } // End using string_format
    } // End using font
} // End DrawText

The code starts by defining the text it will display. It then creates Font and StringFormat objects to use when drawing text.

The code sets the StringFormat object’s Trimming property to Word so the text breaks at a word boundary if it doesn’t fit in the left PictureBox. See the post Perform line trimming in C# for more information on string trimming.

Next the code makes a Bitmap and an associated Graphics object to draw the text in the left PictureBox. It makes a Rectangle representing the location where the text should be drawn and outlines it in light green so you can see it.

The code then uses the Graphics object’s MeasureString method to determine how much of the text will fit in the allowed size with the given font. The code then copies that much of the text into the variable text_that_fits and draws the text.

Next the program removes the text that it has already drawn from the string text. It then draws the remainder of the text in the right PictureBox.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in fonts, graphics, strings and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *