Returns the length of text that could be drawn.

 

   
Syntax
 
     

[Visual Basic]
Function TextLength(inText As String, inParams As String, ByRef outC2 As Integer) As Integer

[C#]
int TextLength(string inText, string inParams, ref int outC2);

 

   
Params
 
     
Name   Description
inText The text to be measured.
inParams A parameter string containing Override parameters, Positioning parameters and Transform parameters.
outC2 On return set to the number of characters that could be drawn.
return The number of characters that could be drawn.

 

   
Notes
 
     

This method takes the same arguments as the DrawText method. However it measures the number of characters that could be drawn into the specified rectangle rather than actually drawing any text.

 

   
See Also
 
     

Canvas DrawText function.
Canvas TextArea function.

 

   
Example
 
     

[Visual Basic]
Dim ca As New Canvas
Dim txt, pars As String
Dim len As Integer
txt = "In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with..."
pars = "Rect=10,10,190,190"
ca.Color = System.Drawing.Color.Yellow
ca.Width = 200
ca.Height = 200
ca.TextFont = "Times"
ca.TextSize = 18
ca.TextLength(txt, pars, len)
ca.DrawText(txt, pars)
ca.SaveAs(Server.MapPath("textlen.jpg"), "Quality=high")
Response.Write("Only able to fit " + len.ToString() + " characters onto the canvas<br>")
Response.Write("<b>Original Text</b><br> " + txt + "<br>")
Response.Write("<b>Drawn Text</b><br> " + Left(txt, len) + "<br>")

[C#]
Canvas ca = new Canvas();
int len = 0;
string txt = "In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with...";
string pars = "Rect=10,10,190,190";
ca.Color = System.Drawing.Color.Yellow;
ca.Width = 200;
ca.Height = 200;
ca.TextFont = "Times";
ca.TextSize = 18;
ca.TextLength(txt, pars, out len);
ca.DrawText(txt, pars);
ca.SaveAs(Server.MapPath("textlen.jpg"), "Quality=high");
Response.Write("Only able to fit " + len.ToString() + " characters onto the canvas<br>");
Response.Write("<b>Original Text</b><br> " + txt + "<br>");
Response.Write("<b>Drawn Text</b><br> " + txt.Substring(0, len) + "<br>");

The code draws out a piece of text onto a rather small canvas. It also works out how much text was drawn onto the canvas and writes it out as HTML. The image saved and the page output is shown below.

textlen.jpg

Only able to fit 106 characters onto the canvas
Original Text
In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with...
Drawn Text
In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms