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

Drawing Text String

To draw single-line text, Graphics Mill for .NET provides method DrawString of the GdiGraphics class. The following should be passed into this method:

  • A text string to draw;
  • A Font class instance initialized by proper font settings (size, name, etc);
  • Coordinates of the text;
  • A solid brush that specifies text color.

Some overloads of this method requires additional parameters to provide more features like outlined text drawing.

Let's examine a simple code example which draws the following text:

Text sample.

Here is a code sample:

Visual Basic
Dim bitmap As New Aurigma.GraphicsMill.Bitmap( _
 Aurigma.GraphicsMill.RgbColor.White, 300, 30, _
 Aurigma.GraphicsMill.PixelFormat.Format24bppRgb)

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

'Draw text
Dim font As New Aurigma.GraphicsMill.Drawing.Font("Futura Md BT", 20)
font.Bold = False
font.Italic = True
font.Underline = True
font.Strikeout = False
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Center

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

graphics.DrawString("Sample text", font, brush, 150, 0)
C#
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(
    Aurigma.GraphicsMill.RgbColor.White, 300, 30, 
    Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);

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

//Draw text
Aurigma.GraphicsMill.Drawing.Font font = 
    new Aurigma.GraphicsMill.Drawing.Font("Futura Md BT", 20);
font.Bold = false;
font.Italic = true;
font.Underline = true;
font.Strikeout = false;
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Center;

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

graphics.DrawString("Sample text", font, brush, 150, 0);

Coordinates of the text can be treated in different ways. The interpretation of these coordinates depend on the text alignment (horizontal and vertical). Horizontal alignment can be modified with HorizontalAlignment property of the Font class. It specifies whether X coordinate is a beginning (left position), center, or end (right position) of the text:

Horizontal text alignment

This figure was produced with the following code:

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

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

Dim pen As New Aurigma.GraphicsMill.Drawing.Pen(Aurigma.GraphicsMill.RgbColor.Red, 1)

graphics.DrawLine(pen, 50, 0, 50, 100)

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

' Adjust font settings
Dim font As New Aurigma.GraphicsMill.Drawing.Font("Arial", 20, False, False)

'Draw text with different horizontal alignments        
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Left
graphics.DrawString("Left", font, brush, 50, 0)

font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Center
graphics.DrawString("Center", font, brush, 50, 33)

font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Right
graphics.DrawString("Right", font, brush, 50, 63)
C#
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(
    Aurigma.GraphicsMill.RgbColor.White, 100, 100, 
    Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);

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

Aurigma.GraphicsMill.Drawing.Pen pen = new Aurigma.GraphicsMill.Drawing.Pen(
    Aurigma.GraphicsMill.RgbColor.Red, 1);

graphics.DrawLine(pen, 50, 0, 50, 100);

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

//Adjust font settings
Aurigma.GraphicsMill.Drawing.Font font = 
    new Aurigma.GraphicsMill.Drawing.Font("Arial", 20, false, false);

//Draw text with different horizontal alignments        
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Left;
graphics.DrawString("Left", font, brush, 50, 0);

font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Center;
graphics.DrawString("Center", font, brush, 50, 33);

font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Right;
graphics.DrawString("Right", font, brush, 50, 63);

Vertical alignment can be modified with VerticalAlignment property of the Font class. It specifies whether Y coordinate specifies top, bottom or baseline level of the text:

Vertical text alignment

This figure was created with the following code:

Visual Basic
Dim bitmap As New Aurigma.GraphicsMill.Bitmap( _
 Aurigma.GraphicsMill.RgbColor.White, 200, 60, _
 Aurigma.GraphicsMill.PixelFormat.Format24bppRgb)

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

Dim pen As New Aurigma.GraphicsMill.Drawing.Pen(Aurigma.GraphicsMill.RgbColor.Red, 1)

graphics.DrawLine(pen, 0, 30, 200, 30)

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

' Adjust font settings
Dim font As New Aurigma.GraphicsMill.Drawing.Font("Arial", 20, False, False)
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Left

'Draw text with different vertical alignments            
font.VerticalAlignment = Aurigma.GraphicsMill.Drawing.VerticalAlignment.Bottom
graphics.DrawString("Bottom", font, brush, 0, 30)

font.VerticalAlignment = Aurigma.GraphicsMill.Drawing.VerticalAlignment.Baseline
graphics.DrawString("Baseline", font, brush, 80, 30)

font.VerticalAlignment = Aurigma.GraphicsMill.Drawing.VerticalAlignment.Top
graphics.DrawString("Top", font, brush, 160, 30)
C#
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(
    Aurigma.GraphicsMill.RgbColor.White, 200, 60, 
    Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);

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

Aurigma.GraphicsMill.Drawing.Pen pen = 
    new Aurigma.GraphicsMill.Drawing.Pen(Aurigma.GraphicsMill.RgbColor.Red, 1);

graphics.DrawLine(pen, 0, 30, 200, 30);

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

//Adjust font settings
Aurigma.GraphicsMill.Drawing.Font font = 
    new Aurigma.GraphicsMill.Drawing.Font("Arial", 20, false, false);
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Left;

//Draw text with different vertical alignments            
font.VerticalAlignment = Aurigma.GraphicsMill.Drawing.VerticalAlignment.Bottom;
graphics.DrawString("Bottom", font, brush, 0, 30);

font.VerticalAlignment = Aurigma.GraphicsMill.Drawing.VerticalAlignment.Baseline;
graphics.DrawString("Baseline", font, brush, 80, 30);

font.VerticalAlignment = Aurigma.GraphicsMill.Drawing.VerticalAlignment.Top;
graphics.DrawString("Top", font, brush, 160, 30);

To draw rotated text, like this:

Rotated text sample.

you should set rotation matrix into Transform property of the GdiGraphics before drawing the text:

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

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

Dim pen As New Aurigma.GraphicsMill.Drawing.Pen(Aurigma.GraphicsMill.RgbColor.Black, 1)

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

'Adjust font settings
Dim font As New Aurigma.GraphicsMill.Drawing.Font("Tahoma", 20)
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Center
font.VerticalAlignment = Aurigma.GraphicsMill.Drawing.VerticalAlignment.Bottom

'Draw rotated line and text
Dim matrix As New System.Drawing.Drawing2D.Matrix
matrix.RotateAt(45, New System.Drawing.PointF(60, 60))
graphics.Transform = matrix

graphics.DrawLine(pen, -30, 60, 150, 60)
graphics.DrawString("Sample text", font, brush, 60, 60)
C#
//Create Bitmap object
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(
    Aurigma.GraphicsMill.RgbColor.White, 120, 120, 
    Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);

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

Aurigma.GraphicsMill.Drawing.Pen pen = 
    new Aurigma.GraphicsMill.Drawing.Pen(Aurigma.GraphicsMill.RgbColor.Black, 1);

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

//Adjust font settings
Aurigma.GraphicsMill.Drawing.Font font = new Aurigma.GraphicsMill.Drawing.Font("Tahoma", 20);
font.HorizontalAlignment = Aurigma.GraphicsMill.Drawing.HorizontalAlignment.Center;
font.VerticalAlignment = Aurigma.GraphicsMill.Drawing.VerticalAlignment.Bottom;

//Draw rotated line and text
System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
matrix.RotateAt(45, new System.Drawing.PointF(60, 60));
graphics.Transform = matrix;

graphics.DrawLine(pen, -30, 60, 150, 60);
graphics.DrawString("Sample text", font, brush, 60, 60);
Note

When you try to draw outlined text on a grayscale bitmap (with Format8bppGrayScale pixel format), you will notice that the text is looking ragged (without antialiasing). The reason of this problem is that neither GDI nor GDI+ support grayscale bitmaps, and therefore Graphics Mill for .NET has to imitate it as Format8bppIndexed bitmap with grayscale palette. However, antialiasing is impossible on indexed bitmaps.

See Also

Reference

Manual