Creating a Graphic Container
The BeginContainer method of the Graphics class creates a container. Each BeginContainer method is paired with an EndContainer method.
You can also create nested containers. The following code snippet creates two containers:
Dim gContrainer1 As GraphicsContainer = g.BeginContainer()
' Do something here
Dim gContrainer2 As GraphicsContain1er = g.BeginContainer()
' Do something here
g.EndContainer(gContrainer2)
g.EndContainer(gContrainer1)
Using Graphics Containers to Draw Text
As mentioned earlier, graphics containers are temporary canvases. Let's see how to set the quality of different text for different containers. Listing 9.15 creates two containers, and each has different properties. The first container sets the TextRenderingHint property to AntiAlias and the TextContrast property to 4. The second container sets TextRenderingHint to AntiAliasGridFit and TextContrast to 12. After creating Font and SolidBrush objects, we set the TextRenderingHint property of the Graphics object, and then we call DrawString. Finally we call EndContainer to terminate the container scope.
LISTING 9.15: Using different graphics containers to draw text
Private Sub DrawTextMenu_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Create a Graphics object and set its background as the form's background
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
' Create font and brushes
Dim tnrFont As New Font("Times New Roman", 40, FontStyle.Bold, GraphicsUnit.Pixel)
Dim blueBrush As New SolidBrush(Color.Blue)
g.TextRenderingHint = TextRenderingHint.SystemDefault
' First container boundary starts here
Dim gContrainer1 As GraphicsContainer = g.BeginContainer()
' Gamma correction value = 0 â€" 12. Default is 4.
g.TextContrast = 4
g.TextRenderingHint = TextRenderingHint.AntiAlias
g.DrawString("Text String", tnrFont, blueBrush, New PointF(10, 20))
' Second container boundary starts here
Dim gContrainer2 As GraphicsContainer = g.BeginContainer()
' Gamma correction value = 0 12. Default is 4.
g.TextContrast = 12
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.DrawString("Text String", tnrFont, blueBrush, New PointF(10, 50))
' Second container boundary finishes here
g.EndContainer(gContrainer2)
' First container boundary finishes here
g.EndContainer(gContrainer1)
'Draw string outside of the container
g.DrawString("Text String", tnrFont, blueBrush, New Point(10, 80))
' Dispose of Graphics object
blueBrush.Dispose()
g.Dispose()
End Sub
Note: The TextRenderingHint enumeration is defined in the System.Drawing.Text namespace. Don't forget to add this namespace reference.
Figure 9.23 shows the output from Listing 9.15. Notice the quality difference in the text.
Using Graphics Containers to Draw Shapes
In the previous section we saw how we can use containers to draw text with different rendering quality and performance. We can draw other shapes using SmoothingMode, CompositingQuality, and other properties.
Listing 9.16 uses the AntiAlias, GammaCorrected, and HighSpeed options to draw rectangles and ellipses. We create a container by calling BeginContainer, set the smoothing mode to anti-aliasing, and set the compositing quality and gamma correction of the Graphics object. Then we draw an ellipse and a rectangle. After that we create a second graphics container by making another call to BeginContainer and set the smoothing mode and compositing quality to high speed, and then we draw a new ellipse and rectangle. Finally, we make two calls to the EndContainer method to close the containers.
FIGURE 9.23: Using graphics containers to draw text
LISTING 9.16: Using graphics container to draw shapes
Private Sub DrawShapeMenu_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Create a Graphics object and set its background as the form's background
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
' Create pens
Dim redPen As New Pen(Color.Red, 20)
Dim bluePen As New Pen(Color.Blue, 10)
' Create first graphics container
Dim gContainer1 As GraphicsContainer = g.BeginContainer()
' Set its properties
g.SmoothingMode = SmoothingMode.AntiAlias
g.CompositingQuality = CompositingQuality.GammaCorrected
' Draw graphics objects
g.DrawEllipse(redPen, 10, 10, 100, 50)
g.DrawRectangle(bluePen, 210, 0, 100, 100)
' Create second graphics container
Dim gContainer2 As GraphicsContainer = g.BeginContainer()
' Set its properties
g.SmoothingMode = SmoothingMode.HighSpeed
g.CompositingQuality = CompositingQuality.HighSpeed
' Draw graphics objects
g.DrawEllipse(redPen, 10, 150, 100, 50)
g.DrawRectangle(bluePen, 210, 150, 100, 100)
' Destroy containers
g.EndContainer(gContainer2)
g.EndContainer(gContainer1)
' Dispose of object
redPen.Dispose()
bluePen.Dispose()
g.Dispose()
End Sub
Figure 9.24 shows the output from Listing 9.16. The first ellipse and rectangle are smoother than the second set.
Graphics containers are also useful when you need to render large images either with high quality or at high speed. For example, if you have two large images and only one is quality-sensitive, you can create two graphics containers and set high quality for the first container and high speed for the second.
FIGURE 9.24: Using graphics containers to draw shapes
Conclusion
Hope the article would have helped you in understanding working with Graphics Containers in GDI+. Read other articles on GDI+ on the website.