Export (0) Print
Expand All

Graphics.EndContainer Method

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

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

public:
void EndContainer (
	GraphicsContainer^ container
)
public void EndContainer (
	GraphicsContainer container
)
public function EndContainer (
	container : GraphicsContainer
)
Not applicable.

Parameters

container

GraphicsContainer that represents the container this method restores.

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

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

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

Calls to the Save method place information blocks on the same stack as calls to the BeginContainer method. Just as an EndContainer method call is paired with a BeginContainer method call, a Restore method call is paired with a Save method call.

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

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Opens a new graphics container and saves the old container.

  • Translates the world coordinates in the container.

  • Fills a red rectangle in the (translated coordinates of the) new container.

  • Closes the new container and restores the saved container.

  • Fills a green rectangle (to the untranslated coordinates) of the saved container.

The result is a green rectangle that overlies a red rectangle of the same size.

public:
   void EndContainerState( PaintEventArgs^ e )
   {
      // Begin graphics container.
      GraphicsContainer^ containerState = e->Graphics->BeginContainer();

      // Translate world transformation.
      e->Graphics->TranslateTransform( 100.0F, 100.0F );

      // Fill translated rectangle in container with red.
      e->Graphics->FillRectangle( gcnew SolidBrush( Color::Red ), 0, 0, 200, 200 );

      // End graphics container.
      e->Graphics->EndContainer( containerState );

      // Fill untransformed rectangle with green.
      e->Graphics->FillRectangle( gcnew SolidBrush( Color::Green ), 0, 0, 200, 200 );
   }

public void EndContainerState(PaintEventArgs e) 
{
    // Begin graphics container.
    GraphicsContainer containerState = e.get_Graphics().BeginContainer();

    // Translate world transformation.
    e.get_Graphics().TranslateTransform(100, 100);

    // Fill translated rectangle in container with red.
    e.get_Graphics().FillRectangle(new SolidBrush(Color.get_Red()), 0, 0,
        200, 200);

    // End graphics container.
    e.get_Graphics().EndContainer(containerState);

    // Fill untransformed rectangle with green.
    e.get_Graphics().FillRectangle(new SolidBrush(Color.get_Green()), 0, 0,
        200, 200);
} //EndContainerState

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft