---
title: "Bounded Text"
canonical: https://www.graphicsmill.com/samples/Text/TextLayout/BoundedText_cs
product: "Graphics Mill"
source: "Text/TextLayout/BoundedText.cs"
---

# Bounded Text

Demonstrates how to use bounded text.

## Instruction

This code demonstrates how to draw bounded text inside a specific rectangular area.
It uses the `BoundedText` class to ensure that the text fits within the defined rectangle, and it draws the text using a specific font and brush.
A blue rectangle outlines the bounded text area to visually indicate the space allocated for the text.

## Usings

```csharp
using Aurigma.GraphicsMill.Drawing;
```

## Code Snippet

```csharp
using (var bitmap = new Bitmap(250, 250, PixelFormat.Format24bppRgb, new RgbColor(255, 255, 255, 255)))
using (var graphics = bitmap.GetGraphics())
{
    var brush = new SolidBrush(new RgbColor(0x41, 0x41, 0x41));

    var boundedTextRect = new System.Drawing.RectangleF(20f, 20f, 180f, 150f);

    var text = new BoundedText(Texts.LoremIpsum, graphics.CreateFont("Verdana", 14f), brush)
    {
        Rectangle = boundedTextRect,
    };

    graphics.DrawText(text);

    graphics.DrawRectangle(new Pen(new RgbColor(0x4e, 0xb5, 0xe6)), boundedTextRect);

    bitmap.Save("BoundedText.png");
}
```

## Explanation

The code:

- Creates a 250x250 pixel bitmap with a white background.
- Defines a rectangle for bounding the text (20x20 to 180x150).
- Uses the `BoundedText` class to set the text inside this rectangle, using the "Verdana" font and a gray brush.
- The text is drawn inside the bounded area with automatic wrapping, if necessary.
- A blue rectangle is drawn around the area, highlighting the text's boundaries.
- The final image is saved as "BoundedText.png".

## Tags

- [Drawing](<https://www.graphicsmill.com/samples/tags/drawing_md>): Drawing vector shapes and text
- [Text Layout](<https://www.graphicsmill.com/samples/tags/text-layout_md>): Controlling advanced text layout

## Output

- [BoundedText.png](<https://www.graphicsmill.com/samples/Text/compiled/Debug/BoundedText_png>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Bitmap.GetGraphics](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Bitmap_GetGraphics.htm.md>)
- [Aurigma.GraphicsMill.Drawing.BoundedText](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_BoundedText.htm.md>)
- [Aurigma.GraphicsMill.Drawing.BoundedText.Rectangle](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_BoundedText_Rectangle.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.CreateFont(String,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_CreateFont_2_1dbfa9fa.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawRectangle(Pen,RectangleF)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawRectangle_2_169e24a7.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawText(Text)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawText_1_f576d03f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Pen](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_Pen.htm.md>)
- [Aurigma.GraphicsMill.Drawing.SolidBrush](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_SolidBrush.htm.md>)
