Measuring Text Size

Warning

The text API described in this topic is out of date. If you need to measure text using the new drawing engine in Graphics Mill, read the Fonts and Measuring Text article.

When we draw text combined with graphics, it is very important to be able to measure the text size taking into account font settings. This topic describes how to get the extent of single-line and multiline text using Graphics Mill.

Measuring Single-line Text Size

Measuring the extent of the text string drawn by the Graphics.DrawString method with Graphics Mill is very easy. Just specify the font name, size and other settings in the Font class and use the Graphics.MeasureString(String, Font) method to get a width and height measurement for the string. This figure:

Measuring single-line text example.

can be produced with following code:

C#
using (var bitmap = new Bitmap(250, 40, PixelFormat.Format24bppRgb, RgbColor.White))
using (var graphics = bitmap.GetGraphics())
{
   
//define font and color settings
   
var Timesfont = new Font("Times New Roman", 25);
   
Timesfont.Italic = true;
   
var tahomaFont = new Font("Tahoma", 12);

   
var blackBrush = new SolidBrush(RgbColor.Black);
   
var redBrush = new SolidBrush(RgbColor.Red);

   
string text = "Single-line text...";

   
//Draw text
    graphics
.DrawString(text, Timesfont, blackBrush, 0, 0);

   
// Measure the text size.
   
var size = Graphics.MeasureString(text, Timesfont);

   
//Draw the text size
    text
= "Height: " + size.Height.ToString();
    graphics
.DrawString(text, tahomaFont, redBrush, size.Width + 1, 20);

    text
= "Width: " + size.Width.ToString();
    graphics
.DrawString(text, tahomaFont, redBrush, size.Width + 1, 0);

    bitmap
.Save(@"Images\Output\measuringtextsize1.png");
}

Measuring Multiline Text Size

Graphics Mill provides the Graphics.DrawText method for drawing single-style multiline text. To measure the text's extent you can use the Graphics.MeasureText method. This method accepts the following parameters:

  • Multiline text you want to measure.
  • Font settings.
  • Size of the bounding rectangle.
  • Boolean value specifying whether to clip the text when it does not fit the bounding rectangle.
  • The number of whitespaces to replace one tabulation character in the text with.

To get a valid text size, parameter values of this method should be identical to respective parameters in the Graphics.DrawText method.

The code example below draws the following figure:

Measuring multiline text example.

C#
using (var bitmap = new Bitmap(220, 100, PixelFormat.Format24bppRgb, RgbColor.White))
using (var graphics = bitmap.GetGraphics())
{
   
//define font and color settings
   
var font = new Font("Tahoma", 12);

   
var blackBrush = new SolidBrush(RgbColor.Black);
   
var redBrush = new SolidBrush(RgbColor.Red);

   
var rect = new System.Drawing.Rectangle(0, 0, 150, 100);

   
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.";

   
//Draw text
    graphics
.DrawText(text, font, blackBrush, rect, true, 0);

   
// Measure the text size.
   
var size = Graphics.MeasureText(text, font, rect.Size, true, 0);

   
//Draw the text size
    text
= "Height: " + size.Height.ToString();
    graphics
.DrawString(text, font, redBrush, size.Width + 1, 0);

    text
= "Width: " + size.Width.ToString();
    graphics
.DrawString(text, font, redBrush, size.Width + 1, 20);

    bitmap
.Save(@"Images\Output\measuringtextsize2.png");
}

Measuring Formatted Text Size

To draw and measure multi-style text Graphics Mill provides the Graphics.DrawFormattedText and Graphics.MeasureFormattedText methods, respectively. Both these methods are similar to the corresponding ones intended for single-style text. Similarly to Graphics.MeasureText, the Graphics.MeasureFormattedText method should accept the same respective arguments as the Graphics.DrawFormattedText.

The figure below can be drawn using the following code sample:

Measuring formatted text example.

C#
using (var bitmap = new Bitmap(220, 100, PixelFormat.Format24bppRgb, RgbColor.White))
using (var graphics = bitmap.GetGraphics())
{
   
//define font and color settings
   
var font = new Font("Tahoma", 12);

   
var blackBrush = new SolidBrush(RgbColor.Black);
   
var redBrush = new SolidBrush(RgbColor.Red);

   
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, 150, 100);

   
string text = "<root><para style=\"_line-spacing:10px\"><span style=\"font-weight:bold\">" +
         
"Aurigma Graphics Mill</span> 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.</para></root>";

   
//Draw text
    graphics
.DrawFormattedText(text, font, blackBrush, rect, true, 0);

   
// Measure the text size.
   
var size = Graphics.MeasureFormattedText(text, font, rect.Size, true, 0);

   
//Draw the text size
    text
= "Height: " + size.Height.ToString();
    graphics
.DrawString(text, font, redBrush, size.Width + 1, 0);

    text
= "Width: " + size.Width.ToString();
    graphics
.DrawString(text, font, redBrush, size.Width + 1, 20);

    bitmap
.Save(@"Images\Output\measuringtextsize3.png");
}

See Also

Reference

Manual