GraphicsPath.GetBounds Method ()


Returns a rectangle that bounds this GraphicsPath.

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

public RectangleF GetBounds()

Return Value

Type: System.Drawing.RectangleF

A RectangleF that represents a rectangle that bounds this GraphicsPath.

The size of the returned bounding rectangle is influenced by the type of end caps, pen width, and pen miter limit, and therefore produces a "loose fit" to the bounded path. The approximate formula is: the initial bounding rectangle is inflated by pen width, and this result is multiplied by the miter limit, plus some additional margin to allow for end caps.

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, an OnPaint event object. The code performs the following actions:

  • Creates a graphics path.

  • Adds an ellipse (circle) to it and draws it to the screen.

  • Retrieves the bounding rectangle for the circle with a call to GetBounds and draws the rectangle to the screen.

  • Creates a second graphics path.

  • Adds a circle and widens the path to a width of 10.

  • Draws the path to the screen.

  • Retrieves the bounding rectangle for the second circle.

  • Draws the bounding rectangle to the screen.

  • Displays the rectangle size in a dialog box.

Notice that the bounding rectangle on the right is larger (to account for the extra width of the line).

public void GetBoundsExample(PaintEventArgs e)

    // Create path number 1 and a Pen for drawing.
    GraphicsPath myPath = new GraphicsPath();
    Pen pathPen = new Pen(Color.Black, 1);

    // Add an Ellipse to the path and Draw it (circle in start

    // position).
    myPath.AddEllipse(20, 20, 100, 100);
    e.Graphics.DrawPath(pathPen, myPath);

    // Get the path bounds for Path number 1 and draw them.
    RectangleF boundRect = myPath.GetBounds();
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1),

    // Create a second graphics path and a wider Pen.
    GraphicsPath myPath2 = new GraphicsPath();
    Pen pathPen2 = new Pen(Color.Black, 10);

    // Create a new ellipse with a width of 10.
    myPath2.AddEllipse(150, 20, 100, 100);
    e.Graphics.FillPath(Brushes.Black, myPath2);

    // Get the second path bounds.
    RectangleF boundRect2 = myPath2.GetBounds();

    // Draw the bounding rectangle.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1),

    // Display the rectangle size.

.NET Framework
Available since 1.1
Return to top