Draws a string of text.
Syntax

[C#]

void DrawString(string text, Font font, Brush brush, double x, double y);
void DrawString(string text, Font font, Brush brush, double x, double y, stringFormat format);
void DrawString(string text, Font font, Brush brush, Pen pen, double x, double y, stringFormat format);
void DrawString(string text, Font font, Brush brush, Pen pen, GraphicsPath path, stringFormat format);
void DrawString(string text, Font font, Brush brush, Pen pen, PointF pt, stringFormat format);
void DrawString(string text, Font font, Brush brush, Pen pen, RectangleF rect, stringFormat format);
void DrawString(string text, Font font, Brush brush, PointF pt, stringFormat format);
void DrawString(string text, Font font, Brush brush, RectangleF rect, stringFormat format);

[Visual Basic]

Sub DrawString(text As String, font As Font, brush As Brush, x As Double, y As Double)
Sub DrawString(text As String, font As Font, brush As Brush, x As Double, y As Double, format As StringFormat)
Sub DrawString(text As String, font As Font, brush As Brush, pen As Pen, x As Double, y As Double, format As StringFormat)
Sub DrawString(text As String, font As Font, brush As Brush, pen As Pen, path As GraphicsPath, format As StringFormat)
Sub DrawString(text As String, font As Font, brush As Brush, pen As Pen, pt As PointF, format As StringFormat)
Sub DrawString(text As String, font As Font, brush As Brush, pen As Pen, rect As RectangleF, format As StringFormat)
Sub DrawString(text As String, font As Font, brush As Brush, pt As PointF, format As StringFormat)
Sub DrawString(text As String, font As Font, brush As Brush, rect As RectangleF, format As StringFormat)
Params
Name Description
text The text that should be drawn.
font The font to use.
brush The brush used to paint the text.
x The x position of the top left of the text.
y The y position of the top left of the text.
format The style and formatting for the text.
pen The pen to use for drawing the outlines of the text.
path A path onto which the text should be fitted.
pt The top left for the text.
rect A rectangle in which the text should be positioned.
Notes

None.

Example

Here we use Language and TextFont to mix standard text and symbol fonts.

[C#]

using (Bitmap bm = new Bitmap(600, 600)) {
  using (Graphics graphics = Graphics.FromImage(bm)) {
    graphics.Clear(Color.Yellow);
    Font font1 = new Font("Times", 48, FontStyle.Regular);
    Font font2 = new Font("Wingdings", 48, FontStyle.Regular);
    graphics.DrawString("Some text...", font1, Brushes.Black, new PointF(0, 0), StringFormat.GenericDefault);
    graphics.DrawString("Symbols.", font2, Brushes.Black, new PointF(0, 120), StringFormat.GenericDefault);
    graphics.DrawString("Some more text...", font1, Brushes.Black, new PointF(0, 240), StringFormat.GenericDefault);
  }
  bm.Save(Server.MapPath("IG8_Graphics_DrawString.png"));
}


It produces the following output image:


IG8_Graphics_DrawString.png

Example

[C#]

using (Bitmap bm = new Bitmap(600, 600)) {
  using (Graphics graphics = Graphics.FromImage(bm)) {
    RectangleF rect = bm.Bounds;
    rect.Inflate(-50, -50);
    // draw arc
    GraphicsPath arc = new GraphicsPath();
    arc.AddArc(rect, 20, 300);
    Pen pen = new Pen(Color.Blue, 10);
    pen.StartCap = LineCap.DiamondAnchor;
    pen.EndCap = LineCap.OpenArrowAnchor;
    graphics.DrawPath(pen, arc);
    // draw text
    Font font = new Font("Arial", 56, FontStyle.Regular);
    var f = new StringFormat();
    graphics.DrawString("Near...", font, new SolidBrush(Color.Red), null, arc, f);
    f.LineAlignment = 0.5;
    f.Alignment = 0.5;
    graphics.DrawString("Middle...", font, new SolidBrush(Color.Green), null, arc, f);
    f.LineAlignment = 1;
    f.Alignment = 1;
    graphics.DrawString("Far...", font, new SolidBrush(Color.Blue), null, arc, f);
  }
  bm.Save(Server.MapPath("IG8_GraphicsPath_CurvedText.png"));
}


The code here draws an arc into a GraphicsPath. We draw the arc onto a Bitmap providing arrows at either end. After that we draw some items of text onto the curve using different alignment settings. The final output is shown below.


IG8_GraphicsPath_CurvedText.png