Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Graphics.BeginContainer Method

Saves a graphics container with the current state of this Graphics object and opens and uses a new graphics container.

Overload List

Saves a graphics container with the current state of this Graphics object and opens and uses a new graphics container.

[Visual Basic] Overloads Public Function BeginContainer() As GraphicsContainer
[C#] public GraphicsContainer BeginContainer();
[C++] public: GraphicsContainer* BeginContainer();
[JScript] public function BeginContainer() : GraphicsContainer;

Saves a graphics container with the current state of this Graphics object and opens and uses a new graphics container with the specified scale transformation.

[Visual Basic] Overloads Public Function BeginContainer(Rectangle, Rectangle, GraphicsUnit) As GraphicsContainer
[C#] public GraphicsContainer BeginContainer(Rectangle, Rectangle, GraphicsUnit);
[C++] public: GraphicsContainer* BeginContainer(Rectangle, Rectangle, GraphicsUnit);
[JScript] public function BeginContainer(Rectangle, Rectangle, GraphicsUnit) : GraphicsContainer;

Saves a graphics container with the current state of this Graphics object and opens and uses a new graphics container with the specified scale transformation.

[Visual Basic] Overloads Public Function BeginContainer(RectangleF, RectangleF, GraphicsUnit) As GraphicsContainer
[C#] public GraphicsContainer BeginContainer(RectangleF, RectangleF, GraphicsUnit);
[C++] public: GraphicsContainer* BeginContainer(RectangleF, RectangleF, GraphicsUnit);
[JScript] public function BeginContainer(RectangleF, RectangleF, GraphicsUnit) : GraphicsContainer;

Example

[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates two rectangles to specify a scale transformation for the new container.
  • Opens the new graphics container and saves the old container.
  • Fills a red rectangle in the (scaled coordinates of the) new container.
  • Closes the new container and restores the saved container.
  • Fills a green rectangle (to the unscaled coordinates) of the saved container.

[Visual Basic, C#] The result is a green rectangle that overlies a smaller red rectangle.

[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of BeginContainer. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub BeginContainerRectangleF(e As PaintEventArgs)
' Define transformation for container.
Dim srcRect As New RectangleF(0F, 0F, 200F, 200F)
Dim destRect As New RectangleF(100F, 100F, 150F, 150F)
' Begin graphics container.
Dim containerState As GraphicsContainer = _
e.Graphics.BeginContainer(destRect, srcRect, GraphicsUnit.Pixel)
' Fill red rectangle in container.
e.Graphics.FillRectangle(New SolidBrush(Color.Red), 0F, 0F, _
200F, 200F)
' End graphics container.
e.Graphics.EndContainer(containerState)
' Fill untransformed rectangle with green.
e.Graphics.FillRectangle(New SolidBrush(Color.Green), 0F, 0F, _
200F, 200F)
End Sub
        
[C#] 
public void BeginContainerRectangleF(PaintEventArgs e)
{
// Define transformation for container.
RectangleF srcRect = new RectangleF(0.0F, 0.0F, 200.0F, 200.0F);
RectangleF destRect = new RectangleF(100.0F, 100.0F, 150.0F, 150.0F);
// Begin graphics container.
GraphicsContainer containerState = e.Graphics.BeginContainer(
destRect, srcRect,
GraphicsUnit.Pixel);
// Fill red rectangle in container.
e.Graphics.FillRectangle(new SolidBrush(Color.Red), 0.0F, 0.0F, 200.0F, 200.0F);
// End graphics container.
e.Graphics.EndContainer(containerState);
// Fill untransformed rectangle with green.
e.Graphics.FillRectangle(new SolidBrush(Color.Green), 0.0F, 0.0F, 200.0F, 200.0F);
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Graphics Class | Graphics Members | System.Drawing Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft