Saving and Restoring Graphics States in GDI+

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

The GraphicsState class represents the state of a Graphics object. This class does not have any useful properties or methods, but it is used by the Save and Restore methods of the Graphics object. A call to the Save method saves a GraphicsState object as an information block on the stack and returns it. When this object is passed to the Restore method, the information block is removed from the stack and the graphics state is restored to the saved state.

You can make multiple calls to Save (even nested), and each time a new state will be saved and a new GraphicState object will be returned. When you call Restore, the block will be freed on the basis of the GraphicsState object you pass as a parameter.

Now Let's see how this works in our next example. We create a Windows application, add a MainMenu control and its items, and write click event handlers for these items. Listing 9.14 creates and saves graphics states using the Save method, then restores them one by one. The first saved state stores page units and a rotation transformation; the second state stored a translation transformation. We save the first graphics state as gs1. Then we call the TranslateTransform method, which translates and transforms the graphics object. We save the new graphics state as gs2. Now we call ResetTransform, which removes all the transformation effects. Then we draw an ellipse. We restore the graphics states by calling GraphicsState.Restore methods for both gs1 and gs2, and we fill a rectangle and draw an ellipse, respectively.

LISTING 9.14: Saving and restoring graphics states

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

            // Page transformation
            g.PageUnit = GraphicsUnit.Pixel;

            // World transformation
            g.RotateTransform(45, MatrixOrder.Append);

            // Save first graphics state
            GraphicsState gs1 = g.Save();

            // One more transformation
            g.TranslateTransform(0, 110);

            // Save graphics state again
            GraphicsState gs2 = g.Save();

            // undo all transformation effects by resetting the transformation

            // Draw a simple ellipse with no transformation
            g.DrawEllipse(Pens.Red, 100, 0, 100, 50);

            // Restore first graphics state, which means that the new item should rotate 45 degrees
            g.FillRectangle(Brushes.Blue, 100, 0, 100, 50);

            // Restore second graphics state
            g.DrawEllipse(Pens.Green, 100, 50, 100, 50);
            // Dispose of Graphics object

Figure 9.22 shows the output from Listing 9.14. The first ellipse has no transformation effect, but the rectangle and ellipse below do have transformation effects.

Figure 9.22.jpg

FIGURE 9.22: Saving and restoring graphics states


Hope the article would have helped you in understanding Saving and Restoring Graphics States in GDI+. Read other articles on GDI+ on the website.

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.