Export (0) Print
Expand All

Region.GetBounds Method

Gets a RectangleF structure that represents a rectangle that bounds this Region on the drawing surface of a Graphics object.

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

public RectangleF GetBounds(
	Graphics g
)

Parameters

g
Type: System.Drawing.Graphics

The Graphics on which this Region is drawn.

Return Value

Type: System.Drawing.RectangleF
A RectangleF structure that represents the bounding rectangle for this Region on the specified drawing surface.

ExceptionCondition
ArgumentNullException

g is null.

The current transformation of the graphics context is used to compute the region interior on the drawing surface. The bounding rectangle is not always the smallest possible bounding rectangle depending on the current transformation.

The following code 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 a GraphicsPath and adds an ellipse to it.

  • Fills the path with blue and draws it to the screen.

  • Creates a region that uses the GraphicsPath.

  • Gets the nonexcluded area of the region when combined with the second rectangle.

  • Gets the bounding rectangle for the region and draws it to the screen in red.

public void GetBoundsExample(PaintEventArgs e)
{

    // Create a GraphicsPath and add an ellipse to it.
    GraphicsPath myPath = new GraphicsPath();
    Rectangle ellipseRect = new Rectangle(20, 20, 100, 100);
    myPath.AddEllipse(ellipseRect);

    // Fill the path with blue and draw it to the screen.
    SolidBrush myBrush = new SolidBrush(Color.Blue);
    e.Graphics.FillPath(myBrush, myPath);

    // Create a region using the GraphicsPath.
    Region myRegion = new Region(myPath);

    // Get the bounding rectangle for myRegion and draw it to the 

    // screen in Red.
    RectangleF boundsRect = myRegion.GetBounds(e.Graphics);
    e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(boundsRect));
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft