Graphics Containers in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

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:

            GraphicsContainer gContrainer1 = g.BeginContainer();
            // Do something here
            GraphicsContain1er gContrainer2 = 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 void DrawTextMenu_Click(object sender, System.EventArgs e)
        {
            // Create a Graphics object and set its background as the form's background
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);

            // Create font and brushes
            Font tnrFont = new Font("Times New Roman", 40, FontStyle.Bold, GraphicsUnit.Pixel);
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            g.TextRenderingHint = TextRenderingHint.SystemDefault;
            // First container boundary starts here
            GraphicsContainer gContrainer1 = 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
            GraphicsContainer gContrainer2 = 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();
        }

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.jpg

FIGURE 9.23: Using graphics containers to draw text

LISTING 9.16: Using graphics container to draw shapes

        private void DrawShapeMenu_Click(object sender, System.EventArgs e)
        {
            // Create a Graphics object and set its background as the form's background
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);

            // Create pens
            Pen redPen = new Pen(Color.Red, 20);
            Pen bluePen = new Pen(Color.Blue, 10);

            // Create first graphics container
            GraphicsContainer gContainer1 = 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
            GraphicsContainer gContainer2 = 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();
        }

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.jpg

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.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.