Drawing a String of Text


You can draw a string of text by using the DrawString method. For example, the following statements render the text ASP.NET on an image (see Figure 27.14).

 
 Dim objFont As New Font( "Arial", 16 ) objGraphics.DrawString( "ASP.NET", objFont, Brushes.Blue, 10, 10 ) 
Figure 27.14. Rendering text as an image.

graphics/27fig14.jpg

In this example, the DrawString method accepts five parameters: the text to display, a font, a brush, and the x and y coordinates of the text.

If you need to wrap the text rendered by the DrawString method, you should supply a rectangle that indicates the outer bounds of the text, as shown here:

 
 Dim strString As String = "Here is a very, very, long " & _   "string of text that is used to illustrate how you " & _   "can wrap text rendered by the DrawString method." Dim objFont As New Font( "Arial", 16 ) Dim objRec As RectangleF = New RectangleF( 10, 10, 300, 200 ) objGraphics.DrawString( strString, objFont, Brushes.Blue, objRec ) 

In this example, the text is contained in a 300-pixel-wide by 200-pixel-high rectangle. The text automatically wraps when it reaches the right edge of the rectangle.

Another advantage of rendering text in a rectangle is that you can align the text. The following example centers the text that appears in the rectangle:

 
 Dim strString As String = "Here is a very, very, long " & _   "string of text that is used to illustrate how you " & _   "can center text rendered by the DrawString method." Dim objFont As New Font( "Arial", 16 ) Dim objRec As RectangleF = New RectangleF( 10, 10, 300, 200 ) Dim objFormat As New StringFormat() objFormat.Alignment = StringAlignment.Center objGraphics.DrawString( strString, objFont, Brushes.Blue, objRec, objFormat ) 

Notice that the DrawString method uses a brush. You can use any of the Brush classes with the DrawString method, including TextureBrush or LinearGradientBrush . For example, the following DrawString method uses LinearGradientBrush :

 
[View full width]
 
[View full width]
Dim objFont As New Font( "Arial", 24 ) Dim objRect As New Rectangle( 10, 10, 300, 50 ) Dim objBrush As New LinearGradientBrush( objRect, Color.Red, Color.Yellow, graphics/ccc.gif LinearGradientMode.Vertical ) objGraphics.DrawString( "ASP.NET", objFont, objBrush, 10, 10 )

When working with strings of text, you often need to measure the size of the text before you render it. You can retrieve the width and height of a string of text by using the MeasureString method of the Graphics class like this:

 
 Dim strString As String = "Here is some text!" Dim objFont As New Font( "Arial", 12 ) Dim objTextSize As SizeF objGraphics.DrawString( strString, objFont, Brushes.White, 10, 10 ) objTextSize = objGraphics.MeasureString( strString, objFont ) strString = "Width: " & objTextSize.Width objGraphics.DrawString( strString, objFont, Brushes.White, 10, 50 ) strString = "Height: " & objTextSize.Height objGraphics.DrawString( strString, objFont, Brushes.White, 10, 80 ) 

In the preceding code, the width and height are returned from the MeasureString method as a SizeF structure. The SizeF structure contains both Width and Height properties. The values of both of these properties are displayed.

You can control the quality of the text rendered by the DrawString method by setting the TextRenderingHint property, which accepts any of the following values:

  • AntiAlias

  • AntiAliasGridFit

  • ClearTypeGridFit

  • SingleBitPerPixel

  • SingleBitPerPixelGridFit

  • SystemDefault

For example, to use antialiased text, you set the TextRenderingHint property like this:

 
 objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias 

In general, antialiased text produces the best quality, but it takes longer to render. ClearType text looks best on LCD devices. The effects of different values of the TextRenderingHint property are illustrated in Figure 27.15. (The page used to create this figure is included on the CD with the name TextRenderingHint.aspx .)

Figure 27.15. Effects of TextRenderingHint property.

graphics/27fig15.jpg



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net