This documentation is archived and is not being maintained.

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

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:
void DrawString(
	String^ s, 
	Font^ font, 
	Brush^ brush, 
	RectangleF layoutRectangle, 
	StringFormat^ format
)

Parameters

s
Type: System::String

String to draw.

font
Type: System.Drawing::Font

Font that defines the text format of the string.

brush
Type: System.Drawing::Brush

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

layoutRectangle
Type: System.Drawing::RectangleF

RectangleF structure that specifies the location of the drawn text.

format
Type: System.Drawing::StringFormat

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

ExceptionCondition
ArgumentNullException

brush is nullptr.

-or-

s is nullptr.

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.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: The following code example shows how to center text on the .NET Compact Framework when using DrawString.

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    string s = "Hello\r\nWorld!";
    SolidBrush br = new SolidBrush(System.Drawing.SystemColors.ControlText);

    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Center;
    Rectangle centered = e.ClipRectangle;
    centered.Offset(0, (int)(e.ClipRectangle.Height - e.Graphics.MeasureString(s, this.Font).Height) / 2);

    e.Graphics.DrawString(s, this.Font, br, centered, sf);
}

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, 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:
   void DrawStringRectangleFFormat( PaintEventArgs^ e )
   {
      // Create string to draw.
      String^ drawString = "Sample Text";

      // Create font and brush.
      System::Drawing::Font^ drawFont = gcnew System::Drawing::Font( "Arial",16 );
      SolidBrush^ drawBrush = gcnew 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 = RectangleF(x,y,width,height);

      // Draw rectangle to screen.
      Pen^ blackPen = gcnew Pen( Color::Black );
      e->Graphics->DrawRectangle( blackPen, x, y, width, height );

      // Set format of string.
      StringFormat^ drawFormat = gcnew StringFormat;
      drawFormat->Alignment = StringAlignment::Center;

      // Draw string to screen.
      e->Graphics->DrawString( drawString, drawFont, drawBrush, drawRect, drawFormat );
   }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0
Show: