This documentation is for the old version. Go to the latest Graphics Mill docs

Drawing Multiline Text

When displaying text, you may need to be sure that it will be inside some rectangle. For example, this is necessary when you draw text on a button or in a box. If the text string is longer than this rectangle width, the lines should be automatically wrapped or clipped.

Graphics Mill for .NET provides all these features and allows writing a multiline text using the GdiGraphics class in two ways. The first way is to use the GdiGraphics.DrawText method, which is suitable for drawing strings of simple single-style text. All the font and color settings are defined by the Font and SolidBrush classes properties and are the same for whole text. To get a rectangle the multiline text drawn by this method will occupy, Graphics Mill for .NET provides the Font.MeasureText method. The second way to draw multiline text is represented by the GdiGraphics.DrawFormattedText method. The main difference between this method and the previous one is the possibility to display multi-style text. It means that the GdiGraphics.DrawFormattedText method supports text with portions of different justification, font and color settings. The extent of the text drawn by this method can be measured using the Font.MeasureFormattedText method.

Drawing Text Using the GdiGraphics.DrawText Method

The GdiGraphics.DrawText method accepts the following arguments:

  • Multiline text to draw.
  • Font settings.
  • SolidBrush that specifies the color of the text.
  • The rectangle to draw text in.
  • Boolean value specifying whether to clip the text when it does not fit the bounding rectangle.
  • Number of whitespaces to replace one tabulation character in the text with.

The GdiGraphics.DrawText method supports newline (\n in C# and vbNewLine in VB) and tabulation (\t in C# and vbTab in VB) characters. It makes the characters after the newline symbol be displayed in a new line and replaces the tabulation character with the specified number of whitespaces.

The following code example draws the text box below:

GDI graphics
Visual Basic
'Create Bitmap object
Dim bitmap As New Aurigma.GraphicsMill.Bitmap( _
 Aurigma.GraphicsMill.RgbColor.White, 120, 100, _
 Aurigma.GraphicsMill.PixelFormat.Format24bppRgb)

Dim graphics As Aurigma.GraphicsMill.Drawing.GdiGraphics = bitmap.GetGdiGraphics()

Dim brush As New Aurigma.GraphicsMill.Drawing.SolidBrush(Aurigma.GraphicsMill.RgbColor.Black)

Dim text As String = "Aurigma Graphics Mill is a set of powerful, fast, and " & _
 "memory-friendly components which resolve most image processing requirements: " & _
 "load/save files of most popular formats, enhance, transform, display, " & _
 "reduce colors, combine images, draw imagery, etc."

'Adjust font settings
Dim font As New Aurigma.GraphicsMill.Drawing.Font("Tahoma", 12)
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Left

Dim rect As New System.Drawing.RectangleF(10, 10, 100, 85)

graphics.DrawText(text, font, brush, rect, True, 0)
C#
//Create Bitmap object
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(
    Aurigma.GraphicsMill.RgbColor.White, 120, 100,
    Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);

Aurigma.GraphicsMill.Drawing.GdiGraphics graphics = bitmap.GetGdiGraphics();

Aurigma.GraphicsMill.Drawing.SolidBrush brush =
    new Aurigma.GraphicsMill.Drawing.SolidBrush(Aurigma.GraphicsMill.RgbColor.Black);

string text = "Aurigma Graphics Mill is a set of powerful, fast, and " +
    "memory-friendly components which resolve most image processing requirements: " +
    "load/save files of most popular formats, enhance, transform, display, " +
    "reduce colors, combine images, draw imagery, etc.";

//Adjust font settings
Aurigma.GraphicsMill.Drawing.Font font = new Aurigma.GraphicsMill.Drawing.Font("Tahoma", 12);
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Left;

System.Drawing.RectangleF rect = new System.Drawing.RectangleF(10, 10, 100, 85);

graphics.DrawText(text, font, brush, rect, false, 0);

Drawing Text Using the GdiGraphics.DrawFormattedText Method

This method is quite similar to GdiGraphics.DrawText. It accepts the same parameters; however, the text string you want to draw must be marked up with XML-like tags. This markup specifies text rendering settings for each portion of the text. The XML specification is completely described in the Drawing Formatted Text article.

See Also

Reference

Manual