System.Drawing.Graphics.EndContainer Method

Closes the current graphics container and restores the state of this System.Drawing.Graphics to the state saved by a call to the Graphics.BeginContainer method.

Syntax

public void EndContainer (System.Drawing.Drawing2D.GraphicsContainer container)

Parameters

container
System.Drawing.Drawing2D.GraphicsContainer that represents the container this method restores.

Remarks

Use this method with the Graphics.BeginContainer method to create nested graphics containers. Graphics containers retain graphics state, such as transformation, clipping region, and rendering properties.

When you call the Graphics.BeginContainer method of a System.Drawing.Graphics, an information block that holds the state of the System.Drawing.Graphics is put on a stack. The Graphics.BeginContainer method returns a System.Drawing.Drawing2D.GraphicsContainer that identifies that information block. When you pass the identifying object to the Graphics.EndContainer(System.Drawing.Drawing2D.GraphicsContainer) method, the information block is removed from the stack and is used to restore the System.Drawing.Graphics to the state it was in at the time of the Graphics.BeginContainer method call.

Containers can be nested; that is, you can call the Graphics.BeginContainer method several times before you call the Graphics.EndContainer(System.Drawing.Drawing2D.GraphicsContainer) method. Each time you call the Graphics.BeginContainer method, an information block is put on the stack, and you receive a System.Drawing.Drawing2D.GraphicsContainer for the information block. When you pass one of those objects to the Graphics.EndContainer(System.Drawing.Drawing2D.GraphicsContainer) method, the System.Drawing.Graphics is returned to the state it was in at the time of the Graphics.BeginContainer method call that returned that particular System.Drawing.Drawing2D.GraphicsContainer. The information block placed on the stack by that Graphics.BeginContainer method call is removed from the stack, and all information blocks placed on that stack after that Graphics.BeginContainer method call are also removed.

Calls to the Graphics.Save method place information blocks on the same stack as calls to the Graphics.BeginContainer method. Just as an Graphics.EndContainer(System.Drawing.Drawing2D.GraphicsContainer) method call is paired with a Graphics.BeginContainer method call, a Graphics.Restore(System.Drawing.Drawing2D.GraphicsState) method call is paired with a Graphics.Save method call.

When you call the Graphics.EndContainer(System.Drawing.Drawing2D.GraphicsContainer) method, all information blocks placed on the stack (by the Graphics.Save method or by the Graphics.BeginContainer method) after the corresponding call to the Graphics.BeginContainer method are removed from the stack. Likewise, when you call the Graphics.Restore(System.Drawing.Drawing2D.GraphicsState) method, all information blocks placed on the stack (by the Graphics.Save method or by the Graphics.BeginContainer method) after the corresponding call to the Graphics.Save method are removed from the stack.

Requirements

Namespace: System.Drawing
Assembly: System.Drawing (in System.Drawing.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0