GraphicsPath.AddString Method (String, FontFamily, Int32, Single, Point, StringFormat)

 

Adds a text string to this path.

Namespace:   System.Drawing.Drawing2D
Assembly:  System.Drawing (in System.Drawing.dll)

public void AddString(
	string s,
	FontFamily family,
	int style,
	float emSize,
	Point origin,
	StringFormat format
)

Parameters

s
Type: System.String

The String to add.

family
Type: System.Drawing.FontFamily

A FontFamily that represents the name of the font with which the test is drawn.

style
Type: System.Int32

A FontStyle enumeration that represents style information about the text (bold, italic, and so on). This must be cast as an integer (see the example code later in this section).

emSize
Type: System.Single

The height of the em square box that bounds the character.

origin
Type: System.Drawing.Point

A Point that represents the point where the text starts.

format
Type: System.Drawing.StringFormat

A StringFormat that specifies text formatting information, such as line spacing and alignment.

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, an OnPaint event object. The code performs the following actions:

  • Creates a path.

  • Sets up string and font arguments.

  • Adds the string to the path.

  • Draws the string to the screen.

There are two important things to be pointed out. First, notice that the fontStyle argument is cast as an integer. The AddString method requires this so that two or more FontStyle members can be combined to create the desired font style (in this case, Italic and Underline). Secondly, notice that the FillPath method is used rather than the DrawPath method. If FillPath is used, solid text is rendered, whereas if DrawPath is used, the text will be an outline style.

private void AddStringExample(PaintEventArgs e)
{

    // Create a GraphicsPath object.
    GraphicsPath myPath = new GraphicsPath();

    // Set up all the string parameters.
    string stringText = "Sample Text";
    FontFamily family = new FontFamily("Arial");
    int fontStyle = (int)FontStyle.Italic;
    int emSize = 26;
    Point origin = new Point(20, 20);
    StringFormat format = StringFormat.GenericDefault;

    // Add the string to the path.
    myPath.AddString(stringText,
        family,
        fontStyle,
        emSize,
        origin,
        format);

    //Draw the path to the screen.
    e.Graphics.FillPath(Brushes.Black, myPath);
}

.NET Framework
Available since 1.1
Return to top
Show: