Saving and Restoring GDI+ Graphics States in VB.NET

In this article I will explain about Saving and Restoring Graphics States in GDI+.
  • 2963
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 Sub SaveRestoreMenu_Click(ByVal senderAs Object,ByVal e As System.EventArgs)
        ' Create a Graphics object and set its backgorund as the form's background
        Dim gAs Graphics = Me.CreateGraphics()
        g.Clear(Me.BackColor)

       ' Page transformation
        g.PageUnit = GraphicsUnit.Pixel

       ' World transformation
        g.RotateTransform(45, MatrixOrder.Append)

       ' Save first graphics state
        Dim gs1As GraphicsState = g.Save()

       ' One more transformation
        g.TranslateTransform(0, 110)

       ' Save graphics state again
        Dim gs2As GraphicsState = g.Save()

       ' undo all transformation effects by resetting the transformation
        g.ResetTransform()

       ' 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.Restore(gs1)
        g.FillRectangle(Brushes.Blue, 100, 0, 100, 50)

       ' Restore second graphics state
        g.Restore(gs2)
        g.DrawEllipse(Pens.Green, 100, 50, 100, 50)
        ' Dispose of Graphics object
        g.Dispose()
   End Sub
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.

Figure209_22.jpg
 
FIGURE 9.22: Saving and restoring graphics states
 
Conclusion
 
Hope the article would have helped you in understanding Saving and Restoring Graphics States in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.