Allows you to draw text onto the Canvas.

 

   
Syntax
 
     

[Visual Basic]
Function Canvas.DrawText(inText As String, inParams As String) As System.Drawing.Rectangle

[C#]
System.Drawing.Rectangle Canvas.DrawText(string inText, string inParams);

 

   
Params
 
     
Name   Description
inText The text to be drawn.
inParams A parameter string containing Override parameters, Positioning parameters and Transform parameters.
return

The Rect (before transformation) of the area drawn to.

 

   
Notes
 
     

Allows you to draw text onto the Canvas.

The text is drawn within a rectangle specified in the Parameter String. The font, size, style and color are determined by the current TextFont, TextSize, TextStyle and TextColor properties. The horizontal and vertical alignment of the text within the rectangle is determined by the HAlign and VAlign properties.

Note that if you only specify a position for your text (see example below) the width and height will default to the width and height of the canvas.

To draw curved text or text aligned to a path you should specify a set of points using the Points parameter. As you move left to right the text sits on top of the line and as you move from right to left the text drops down under the line.

There are a variety of ways you can further modify curved text. You can use HAlign to determine whether the text is left, middle or right aligned on the line. Similarly you can use VAlign to control where the text appears relative to the line - top is above, bottom is below, middle means that the base of each character sits on the line. You can further vary this using the Offset parameter which is a percentage of the character height - positive or negative. The Kerning parameter allows you to add or subtract space between characters and is measured in 1000ths of an m-space.

Note that if you use characters outside the normal ASCII range (e.g. for Japanese or other Unicode) you must specify the correct CODEPAGE in your Active Server Pages (e.g. for Japanese CODEPAGE=932) and you may need to specify a TextCharset depending on the type of font you are using.

 

   
See Also
 
     

Canvas TextArea function.
Canvas TextLength function.

 

   
Example
 
     

[Visual Basic]
Dim ca As New Canvas
ca.Color = System.Drawing.Color.Cyan
ca.Width = 300
ca.Height = 300
ca.TextFont = "Times"
ca.TextSize = 18
ca.TextColor = System.Drawing.Color.Gray
ca.DrawText("18 Point Times New Roman", "")
ca.TextSize = 9
ca.TextStyle = "italic"
ca.DrawText("9 Point Italic Times New Roman", "Pos=30,50 TextColor=green")
ca.TextSize = 5
ca.TextStyle = "bold,italic"
ca.DrawText("5 Point Bold Italic Times New Roman", "Pos=60,100 TextColor=gray")
ca.TextSize = 16
ca.DrawText("16 Point Left Aligned Times New Roman", "Pos=10,130 Size=200,200")
ca.DrawText("16 Point Right Aligned Times New Roman", "Pos=10,200 Size=200,200 HAlign=right")
ca.DrawText("16 Point Rotated Text", "Rotate=270 Translate=250,270")
ca.SaveAs(Server.MapPath("drawtext.jpg"), "Quality=high")

[C#]
Canvas ca = new Canvas();
ca.Color = System.Drawing.Color.Cyan;
ca.Width = 300;
ca.Height = 300;
ca.TextFont = "Times";
ca.TextSize = 18;
ca.TextColor = System.Drawing.Color.Gray;
ca.DrawText("18 Point Times New Roman", "");
ca.TextSize = 9;
ca.TextStyle = "italic";
ca.DrawText("9 Point Italic Times New Roman", "Pos=30,50 TextColor=green");
ca.TextSize = 5;
ca.TextStyle = "bold,italic";
ca.DrawText("5 Point Bold Italic Times New Roman", "Pos=60,100 TextColor=gray");
ca.TextSize = 16;
ca.DrawText("16 Point Left Aligned Times New Roman", "Pos=10,130 Size=200,200");
ca.DrawText("16 Point Right Aligned Times New Roman", "Pos=10,200 Size=200,200 HAlign=right");
ca.DrawText("16 Point Rotated Text", "Rotate=270 Translate=250,270");
ca.SaveAs(Server.MapPath("drawtext.jpg"), "Quality=high");

The code draws text in a number of sizes, shapes, styles and positions. It then saves the resulting Canvas out as a jpeg. The final image is shown below.

drawtext.jpg

 

   
Example
 
     

[Visual Basic]
Dim theText1, theText2, thePoints As String
Dim i As Integer
Dim x, y As Double
theText1 = "Once upon a time there was a hobbit who lived in a hole."
theText2 = "Not a dry sandy hole..."
thePoints = ""
For i = 0 To 126
  If thePoints <> "" Then thePoints = thePoints + ","
  x = (120 * Math.Cos(i / 20)) + 150
  y = (120 * Math.Sin(i / 20)) + 150
  thePoints = thePoints + Math.Round(x, 1).ToString() + "," + Math.Round(y, 1).ToString()
Next

Dim thePic As New Canvas
thePic.Color = System.Drawing.Color.Yellow
thePic.TextSize = 18
thePic.Width = 300
thePic.Height = 300
thePic.DrawShape("poly", "Operation=draw Points=" + thePoints)
thePic.DrawText(theText1, "Valign=middle Points=" + thePoints)
thePic.DrawText(theText2, "Offset=-100 Kerning=100 TextColor=red Points=" + thePoints)
thePic.SaveAs(Server.MapPath("shapetext.gif"), "")

[C#]
string theText1 = "Once upon a time there was a hobbit who lived in a hole.";
string theText2 = "Not a dry sandy hole...";
string thePoints = "";
for (int i = 0; i <= 126; i++) {
  if (thePoints != "") thePoints = thePoints + ",";
  double x = (120 * Math.Cos(i / 20)) + 150;
  double y = (120 * Math.Sin(i / 20)) + 150;
  thePoints = thePoints + Math.Round(x, 1).ToString() + "," + Math.Round(y, 1).ToString();
}

Canvas thePic = new Canvas();
thePic.Color = System.Drawing.Color.Yellow;
thePic.TextSize = 18;
thePic.Width = 300;
thePic.Height = 300;
thePic.DrawShape("poly", "Operation=draw Points=" + thePoints);
thePic.DrawText(theText1, "Valign=middle Points=" + thePoints);
thePic.DrawText(theText2, "Offset=-100 Kerning=100 TextColor=red Points=" + thePoints);
thePic.SaveAs(Server.MapPath("shapetext.gif"), "");

The code draws text aligned along a circular path. The final image is shown below.

shapetext.gif