Graphics.DrawString Method (String, Font, Brush, RectangleF, StringFormat)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Draws the specified text string in the specified rectangle with the specified Brush and Font objects using the formatting attributes of the specified StringFormat.

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

Public Sub DrawString (
	s As String,
	font As Font,
	brush As Brush,
	layoutRectangle As RectangleF,
	format As StringFormat


Type: System.String

String to draw.

Type: System.Drawing.Font

Font that defines the text format of the string.

Type: System.Drawing.Brush

Brush that determines the color and texture of the drawn text.

Type: System.Drawing.RectangleF

RectangleF structure that specifies the location of the drawn text.

Type: System.Drawing.StringFormat

StringFormat that specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.

Exception Condition

brush is null.


s is null.

The text represented by the s parameter is drawn inside the rectangle represented by the layoutRectangle parameter. If the text does not fit inside the rectangle, it is truncated at the nearest word, unless otherwise specified with the format parameter.

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a text string to draw.

  • Defines the font as Arial (16pt).

  • Creates a solid, black brush to draw with.

  • Creates a rectangle in which to draw the text.

  • Draws the rectangle to the screen.

  • Sets the format of the string to center it within the rectangle.

  • Draws the string to the screen using the font, brush, and destination rectangle.

Public Sub DrawStringRectangleFFormat(ByVal e As PaintEventArgs)

    ' Create string to draw.
    Dim drawString As [String] = "Sample Text"

    ' Create font and brush.
    Dim drawFont As New Font("Arial", 16)
    Dim drawBrush As New SolidBrush(Color.Black)

    ' Create rectangle for drawing.
    Dim x As Single = 150.0F
    Dim y As Single = 150.0F
    Dim width As Single = 200.0F
    Dim height As Single = 50.0F
    Dim drawRect As New RectangleF(x, y, width, height)

    ' Draw rectangle to screen.
    Dim blackPen As New Pen(Color.Black)
    e.Graphics.DrawRectangle(blackPen, x, y, width, height)

    ' Set format of string.
    Dim drawFormat As New StringFormat
    drawFormat.Alignment = StringAlignment.Center

    ' Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, _
    drawRect, drawFormat)
End Sub

.NET Framework
Available since 1.1
Return to top