Meet us at PRINT 19. Chicago, IL. Oct. 3 - 5.
This documentation is for the old version. Go to the latest Graphics Mill docs

Drawing Text with Outline

To make a text caption more attractive and eye-catching, you can use such efficient technique as drawing this text outlined. This article discusses how outlined text may be used in Graphics Mill for .NET.

To draw the outlined text with Graphics Mill for .NET, you should just use class GdiGraphics. It has a method DrawString which has an overloaded version that takes Pen and Brush as arguments. When you use it, outlined text is drawn:

Outline text using GDI graphics

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

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

Dim font As New Aurigma.GraphicsMill.Drawing.Font("Arial", 50, False, False)

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

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

'Draw both filled and outlined text
graphics.DrawString("Sample text", font, pen, brush, 20, 20)
C#
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(
    Aurigma.GraphicsMill.RgbColor.White, 320, 100, 
    Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);

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

Aurigma.GraphicsMill.Drawing.Font font = new Aurigma.GraphicsMill.Drawing.Font(
    "Arial", 50, false, false);

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

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

//Draw both filled and outlined text
graphics.DrawString("Sample text", font, pen, brush, 20, 20);

As GDI does not support antialiasing when drawing curves, the text is looking quite ragged. Of course, it may be treated as a special effect in few cases, however typically you would expect the text as smooth as possible.

To draw smooth text, we need to use drawing functionality of the GDI+ (i.e. draw the outlined text through standard System.Drawing.Graphics class). It is done in the following way: you create a graphics path based on necessary string, and draw this path at the System.Drawing.Graphics obtained from Bitmap.

As you can see at the figure below, the outlined text drawn with this way is looking much better:

Outline text using GDI+ graphics

The code below produced this image:

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

Dim path As New System.Drawing.Drawing2D.GraphicsPath
path.AddString("Sample text", New System.Drawing.FontFamily("Arial"), _
    0, 50, New System.Drawing.PointF(20, 20), System.Drawing.StringFormat.GenericDefault)

Dim graphics As System.Drawing.Graphics = bitmap.GetGdiplusGraphics()
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias

Dim brush As New System.Drawing.SolidBrush(System.Drawing.Color.Yellow)
graphics.FillPath(brush, path)

Dim pen As New System.Drawing.Pen(System.Drawing.Color.Red, 1)
graphics.DrawPath(pen, path)
C#
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(
    Aurigma.GraphicsMill.RgbColor.White, 320, 100, 
    Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);

System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddString("Sample text", new System.Drawing.FontFamily("Arial"), 
    0, 50, new System.Drawing.PointF(20, 20), System.Drawing.StringFormat.GenericDefault);

System.Drawing.Graphics graphics = bitmap.GetGdiplusGraphics();
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Yellow);
graphics.FillPath(brush, path);

System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);
graphics.DrawPath(pen, path);

If we modify this code, we will be able to draw more advanced outlines. For example, let's examine how to draw the following figure:

Complex outline text using GDI+ graphics

This text is drawn in 3 phases:

  • Draw text with blue fill and red outline with width equal to 4 pixels;
  • Draw yellow outline only (without fill) with width equal to 2 pixels at the same position. This way we get two-ply outline with red and yellow colors.
  • Apply Glow effect.
Visual Basic
Dim bitmap As New Aurigma.GraphicsMill.Bitmap( _
    Aurigma.GraphicsMill.Color.FromArgb32(0, 255, 255, 255), _
    540, 150, Aurigma.GraphicsMill.PixelFormat.Format32bppArgb)

Dim path As New System.Drawing.Drawing2D.GraphicsPath
path.AddString("Sample text", New System.Drawing.FontFamily("Arial"), _
    1, 80, New System.Drawing.Point(20, 20), System.Drawing.StringFormat.GenericDefault)

Dim graphics As System.Drawing.Graphics = bitmap.GetGdiplusGraphics()
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias

Dim brush As New System.Drawing.SolidBrush(System.Drawing.Color.Blue)
graphics.FillPath(brush, path)

Dim redPen As New System.Drawing.Pen(System.Drawing.Color.Red, 4)
graphics.DrawPath(redPen, path)

Dim yellowPen As New System.Drawing.Pen(System.Drawing.Color.Yellow, 2)
graphics.DrawPath(yellowPen, path)

'Apply glow effect
bitmap.Transforms.Glow(Aurigma.GraphicsMill.RgbColor.Blue, 1, 4, False)

'Convert to 24 bit images (merge alpha channel with white background)
bitmap.Channels.DiscardAlpha(Aurigma.GraphicsMill.RgbColor.White)
C#
Aurigma.GraphicsMill.Bitmap bitmap  = new Aurigma.GraphicsMill.Bitmap(
    Aurigma.GraphicsMill.Color.FromArgb32(0, 255, 255, 255), 
    540, 150, Aurigma.GraphicsMill.PixelFormat.Format32bppArgb);

System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddString("Sample text", new System.Drawing.FontFamily("Arial"), 
    1, 80, new System.Drawing.Point(20, 20), System.Drawing.StringFormat.GenericDefault);

System.Drawing.Graphics graphics = bitmap.GetGdiplusGraphics();
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
graphics.FillPath(brush, path);

System.Drawing.Pen redPen = new System.Drawing.Pen(System.Drawing.Color.Red, 4);
graphics.DrawPath(redPen, path);

System.Drawing.Pen yellowPen = new System.Drawing.Pen(System.Drawing.Color.Yellow, 2);
graphics.DrawPath(yellowPen, path);

//Apply glow effect
bitmap.Transforms.Glow(Aurigma.GraphicsMill.RgbColor.Blue, 1, 4, false);

//Convert to 24 bit images (merge alpha channel with white background)
bitmap.Channels.DiscardAlpha(Aurigma.GraphicsMill.RgbColor.White);
Note

When you try to draw outlined text on a grayscale bitmap (with Format8bppGrayScale pixel format), you will notice that antialiasing is not available even if you use GDI+. The reason of this problem is that GDI+ does not 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.