Home

Drawing a String

 

Introduction

Besides the regular geometric and irregular non-geometric shapes we have seen so far, you can draw a string on a graphics object. To support this, the Graphics class is equipped with a method named DrawString that is overloaded with various versions.

One of the versions of the Graphics.DrawString() methods uses the following syntax:

public void DrawString(string s, Font font, Brush brush, float x, float y);

The first argument is the string that will be drawn on the graphics object. The second argument is the definition of the font that will be used. The third argument can simply be a color, it can be a pattern, or it can be a complex texture that will be used to paint the character(s) of the string. The last two arguments are the (top-left) coordinates where the beginning of the string would be.

Here is an example of calling this method:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        Paint += new PaintEventHandler(Painter);
    }

    void Painter(object sender, PaintEventArgs e)
    {
        FontFamily fntFamily = new FontFamily("Times New Roman");
        Font fntWrite = new Font(fntFamily, 24.00F, FontStyle.Regular);

        e.Graphics.DrawString("Rain Drop", fntWrite, Brushes.Blue, 10.00F, 8.00F);
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

This would produce:

Draw String

The above version of the DrawString() method specifies the coordinates as two floating-point numbers. A logical alternative is to consider the coordinates as a PointF value. This can be done using the following version of the method:

public void DrawString(string s, Font font, Brush brush, PointF point);

The first three arguments are the same. The last argument is the PointF combination of the two floating-point values of the previous version of the method.

Confining a String to a Rectangle

One of the advantages of using one of the above versions of the Graphics.DrawString() method is that it would draw the whole string. I you draw other strings, there is a possibility that the strings would overlap. Here is an example:

void Painter(object sender, PaintEventArgs e)
{
    PointF ptF = new PointF(10.00F, 8.00F);
    FontFamily fntFamily = new FontFamily("Times New Roman");
    Font fntWrite = new Font(fntFamily, 18.00F, FontStyle.Regular);

    e.Graphics.DrawString("College Park Auto-Parts", fntWrite, Brushes.Blue, ptF);

    ptF = new PointF(10.00F, 16.00F);
    e.Graphics.DrawString("Georgetown Cleaning Services",
		fntWrite, Brushes.Red, ptF);
}

This would produce:

Draw String

Of course, this means that it is just a concern you would have to address and there are various ways you can solve this type of problem (unless it is your intention to overlap the strings). One alternative you can use is to draw the string inside of a box and not allow it to go beyond that box. To support this, the DrawString() method has the following version:

public void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle);

The last argument is a rectangle value with floating-point numbers. The string would be drawn inside that rectangle. Anything that could go beyond that rectangle would disappear. Here is an example:

void Painter(object sender, PaintEventArgs e)
{
    FontFamily fntFamily = new FontFamily("Times New Roman");
    Font fntWrite = new Font(fntFamily, 20.00F, FontStyle.Regular);
    RectangleF rect = new RectangleF(10.00F, 8.00F, 160.00F, 20.00F);

    e.Graphics.DrawString("College Park Auto-Parts", fntWrite, Brushes.Blue, rect);
}

This would produce:

Draw String

Notice that, not only is the string too tall for the rectangle, but also the string is too long. This means that you must appropriately define the rectangle.

 

Previous Copyright © 2007 FunctionX, Inc. Home