Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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

Draws the specified text string in the specified rectangle with the specified Brush and Font objects.

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

public void DrawString (
	string s,
	Font font,
	Brush brush,
	RectangleF layoutRectangle
)
public void DrawString (
	String s, 
	Font font, 
	Brush brush, 
	RectangleF layoutRectangle
)
public function DrawString (
	s : String, 
	font : Font, 
	brush : Brush, 
	layoutRectangle : RectangleF
)
Not applicable.

Parameters

s

String to draw.

font

Font that defines the text format of the string.

brush

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

layoutRectangle

RectangleF structure that specifies the location of the drawn text.

Exception typeCondition

ArgumentNullException

brush is a null reference (Nothing in Visual Basic).

-or-

s is a null reference (Nothing in Visual Basic).

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. To further manipulate how the string is drawn inside the rectangle use the DrawString overload that takes a StringFormat.

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.

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

public void DrawStringRectangleF(PaintEventArgs e)
{
             
    // Create string to draw.
    String drawString = "Sample Text";
             
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
             
    // Create rectangle for drawing.
    float x = 150.0F;
    float y = 150.0F;
    float width = 200.0F;
    float height = 50.0F;
    RectangleF drawRect = new RectangleF(x, y, width, height);
             
    // Draw rectangle to screen.
    Pen blackPen = new Pen(Color.Black);
    e.Graphics.DrawRectangle(blackPen, x, y, width, height);
             
    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);
}

public void DrawStringRectangleF(PaintEventArgs e) 
{
    // Create string to draw.
    String drawString = "Sample Text";

    // Create font and brush.
    Font drawFont =  new Font("Arial", 16);
    SolidBrush drawBrush =  new SolidBrush(Color.get_Black());

    // Create rectangle for drawing.
    float x = 150;
    float y = 150;
    float width = 200;
    float height = 50;
    RectangleF drawRect =  new RectangleF(x, y, width, height);

    // Draw rectangle to screen.
    Pen blackPen =  new Pen(Color.get_Black());
    e.get_Graphics().DrawRectangle(blackPen, x, y, width, height);

    // Draw string to screen.
    e.get_Graphics().DrawString(drawString, drawFont, drawBrush, drawRect);
} //DrawStringRectangleF

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.