Drawing Text
The Graphics object's DrawString methods render the text to the drawing surface. You pass the font
and color to be used to the DrawString method. For instance, the following code displays the
text "Hello World" by using the form's font and a black brush.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim g As Graphics = e.Graphics
e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)
g.DrawString("Hello World", Me.Font, New SolidBrush(Color.Black), 10,10)
End Sub
VB
Because DrawString takes a brush, it is possible to render text using any brush. This includes a
texture brush. For instance, the following code renders the text with a marbled effect and a
background shadow.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim titleBrush As New TextureBrush(New Bitmap("marble.jpg"))
Dim backgroundBrush As New TextureBrush(New Bitmap("colorbars.jpg"))
Dim titleShadowBrush As New SolidBrush(Color.FromArgb(70, Color.Black))
Dim titleFont As New Font("Lucida Sans Unicode", 60)
Dim titleText As String = "Graphics Samples"
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
e.Graphics.FillRectangle(backgroundBrush, ClientRectangle)
e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle)
e.Graphics.DrawString(titleText, titleFont, titleShadowBrush, 15, 15)
e.Graphics.DrawString(titleText, titleFont, titleBrush, 10, 10)
End Sub
VB
If you supply DrawString with a Rectangle, the text will wrap to fit in the rectangle.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)
Dim textFont As New Font("Lucida Sans Unicode", 12)
Dim rectangle As New RectangleF(100, 100, 250, 350)
e.Graphics.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle)
Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
& "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
& "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
& "language runtime." & ControlChars.CrLf _
& "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
& "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
& "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
& "maintenance costs (TCO) for applications" & ControlChars.CrLf _
& "written in Windows Forms." & ControlChars.CrLf _
& "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
& "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
& "control and container classes. This significantly reduces" & ControlChars.CrLf _
& "control-container interoperability issues." & ControlChars.CrLf
e.Graphics.DrawString(flowedText, textFont, New SolidBrush(Color.Blue), rectangle)
End Sub
VB
You can control how the text is drawn by using the StringFormat object. For instance, the
following code allows you to draw text that is centered in a particular area.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)
Dim textFont As New Font("Lucida Sans Unicode", 8)
Dim rectangle As New RectangleF(100, 100, 250, 350)
e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle)
Dim format As New StringFormat()
format.Alignment = StringAlignment.Center
Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
& "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
& "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
& "language runtime." & ControlChars.CrLf _
& "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
& "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
& "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
& "maintenance costs (TCO) for applications" & ControlChars.CrLf _
& "written in Windows Forms." & ControlChars.CrLf _
& "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
& "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
& "control and container classes. This significantly reduces" & ControlChars.CrLf _
& "control-container interoperability issues." & ControlChars.CrLf
e.Graphics.DrawString(flowedText, textFont, New SolidBrush(Color.Blue), rectangle,format)
End Sub
VB
If you want to find out how long a string will be when it is drawn, you can use MeasureString.
For instance, to center a string on a form, use the following code.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle)
Dim textToDraw As String ="Hello Symetrical World"
Dim textFont As New Font("Lucida Sans Unicode", 8)
Dim windowCenter As Double = this.DisplayRectangle.Width/2
Dim stringSize As SizeF = e.Graphics.MeasureString(textToDraw, textFont)
Dim startPos As Double = windowCenter-(stringSize.Width/2)
e.Graphics.DrawString(textToDraw, textFont, new SolidBrush(Color.Blue), startPos, 40)
End Sub
VB
MeasureString can also be used to determine how many lines and characters will be rendered. For
instance, we can determine how many lines and characters
will be rendered in the previous flowed text example.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle)
Dim textFont As New Font("Lucida Sans Unicode", 12)
Dim rectangle As New RectangleF(100, 100, 250, 350)
Dim lines As Integer
Dim characters As Integer
Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
& "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
& "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
& "language runtime." & ControlChars.CrLf _
& "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
& "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
& "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
& "maintenance costs (TCO) for applications" & ControlChars.CrLf _
& "written in Windows Forms." & ControlChars.CrLf _
& "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
& "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
& "control and container classes. This significantly reduces" & ControlChars.CrLf _
& "control-container interoperability issues." & ControlChars.CrLf
Dim whatRenderedText As String
e.Graphics.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle)
e.Graphics.MeasureString(flowedText, textFont, rectangle.Size, _
new StringFormat(), characters, lines)
whatRenderedText = "We printed " & characters & " characters and " & lines & " lines"
e.Graphics.DrawString(flowedText, textFont, New SolidBrush(Color.Blue), rectangle)
e.Graphics.DrawString(whatRenderedText, Me.Font, New SolidBrush(Color.Black), 10,10)
End Sub
VB
GDI+ has full Unicode support. This means that it is possible to render text in any
language. For instance, the following code draws a string in Japanese (you must
have the Japanese language pack installed).
Protected Overrides Sub OnPaint(e As PaintEventArgs)
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle)
Try
Dim japaneseFont As New Font("MS Mincho", 32)
Dim japaneseText As New String(new char() {CType(31169, Char), CType(12398, Char), _
CType(21517, Char), CType(21069, Char), CType(12399, Char), _
CType(12463, Char), CType(12522, Char), CType(12473, Char), _
CType(12391, Char), CType(12377, Char), CType(12290, Char)})
e.Graphics.DrawString(japaneseText, japaneseFont, new SolidBrush(Color.Blue), 20, 40)
Catch ex As Exception
MessageBox.Show("You need to install the Japanese language pack to run this sample")
Application.Exit()
End Try
End Sub
VB
Using Images
GDI+ has full support for a range of image formats, including .jpeg, .png, .gif, .bmp, .tiff, .exif, and .icon files.
Rendering an image is simple. For instance, the following renders a .jpeg image.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)
e.Graphics.DrawImage(New Bitmap("sample.jpg"), 29, 20, 283, 212)
End Sub
VB
Using a a bitmap as a rendering surface is also simple. The following code renders
some text and lines to a bitmap, and then saves the bitmap to disk as a .png file.
Dim newBitmap As New Bitmap(800,600,PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(newBitmap)
g.FillRectangle(New SolidBrush(Color.White), new Rectangle(0,0,800,600))
Dim textFont As New Font("Lucida Sans Unicode", 12)
Dim rectangle As New RectangleF(100, 100, 250, 350)
Dim Lines As Integer
Dim Characters As Integer
Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
& "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
& "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
& "language runtime." & ControlChars.CrLf _
& "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
& "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
& "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
& "maintenance costs (TCO) for applications" & ControlChars.CrLf _
& "written in Windows Forms." & ControlChars.CrLf _
& "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
& "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
& "control and container classes. This significantly reduces" & ControlChars.CrLf _
& "control-container interoperability issues." & ControlChars.CrLf
g.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle)
g.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle)
Dim penExample As New Pen(Color.FromArgb(150, Color.Purple), 20)
penExample.DashStyle = DashStyle.Dash
penExample.StartCap = LineCap.Round
penExample.EndCap = LineCap.Round
g.DrawCurve(penExample, new Point() { _
new Point(200, 140), _
new Point(700, 240), _
new Point(500, 340), _
new Point(140, 140), _
new Point(40, 340), _
})
newBitmap.Save("TestImage.png", ImageFormat.Png)
VB
Other Information
Standard Pens and Brushes
The Pen and Brush classes include a set of standard solid pens and brushes for all of the
known colors.
Anti-Aliasing
Setting Graphics.SmoothingMode to SmoothingMode.AntiAlias will result in sharper text and graphics.
Scope of the Graphics Object
The graphics object that is contained in the arguments to the Paint event (PaintEventArgs)
is disposed upon return from the Paint event handler. Therefore, you should not keep a reference
to this Graphics object that has scope outside the Paint event. Attempting to use this
Graphics object outside the Paint event will have unpredictable results.
Handling Resize
By default, the Paint event is not raised when a control or form is resized. If you
want the Paint event to be raised when a form is resized, you need to set the
control style appropriately.
Public Sub MyForm()
SetStyle(ControlStyles.ResizeRedraw,True)
End Sub
VB
Copyright 2001 Microsoft Corporation. All rights reserved.