Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
GraphicsPath Class
GetBounds Method
 GetBounds Method ()

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
GraphicsPath.GetBounds Method ()
Returns a rectangle that bounds this GraphicsPath.

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

Visual Basic (Declaration)
Public Function GetBounds As RectangleF
Visual Basic (Usage)
Dim instance As GraphicsPath
Dim returnValue As RectangleF

returnValue = instance.GetBounds
C#
public RectangleF GetBounds ()
C++
public:
RectangleF GetBounds ()
J#
public RectangleF GetBounds ()
JScript
public function GetBounds () : RectangleF
XAML
Not applicable.

Return Value

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).

Visual Basic
Public Sub GetBoundsExample(ByVal e As PaintEventArgs)

    ' Create path number 1 and a Pen for drawing.
    Dim myPath As New GraphicsPath
    Dim pathPen As 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.
    Dim boundRect As RectangleF = myPath.GetBounds()
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), boundRect.X, _
    boundRect.Y, boundRect.Height, boundRect.Width)

    ' Create a second graphics path and a wider Pen.
    Dim myPath2 As New GraphicsPath
    Dim pathPen2 As New Pen(Color.Black, 10)

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

    ' Get the second path bounds.
    Dim boundRect2 As RectangleF = myPath2.GetBounds()

    ' Show the bounds in a message box.
    e.Graphics.DrawString("Rectangle2 Bounds: " + _
    boundRect2.ToString(), New Font("Arial", 8), Brushes.Black, _
    20, 150)

    ' Draw the bounding rectangle.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), boundRect2.X, _
    boundRect2.Y, boundRect2.Height, boundRect2.Width)
End Sub
C#
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),
        boundRect.X,
        boundRect.Y,
        boundRect.Height,
        boundRect.Width);
             
    // 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);
    myPath2.Widen(pathPen2);
    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),
        boundRect2.X,
        boundRect2.Y,
        boundRect2.Height,
        boundRect2.Width);
             
    // Display the rectangle size.
    MessageBox.Show(boundRect2.ToString());
}
C++
public:
   void GetBoundsExample( PaintEventArgs^ e )
   {
      // Create path number 1 and a Pen for drawing.
      GraphicsPath^ myPath = gcnew GraphicsPath;
      Pen^ pathPen = gcnew Pen( Color::Black,1.0f );

      // 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( gcnew Pen( Color::Red,1.0f ), boundRect.X, boundRect.Y, boundRect.Height, boundRect.Width );

      // Create a second graphics path and a wider Pen.
      GraphicsPath^ myPath2 = gcnew GraphicsPath;
      Pen^ pathPen2 = gcnew Pen( Color::Black,10.0f );

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

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

      // Draw the bounding rectangle.
      e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), boundRect2.X, boundRect2.Y, boundRect2.Height, boundRect2.Width );

      // Display the rectangle size.
      MessageBox::Show( boundRect2.ToString() );
   }
J#
public void GetBoundsExample(PaintEventArgs e)
{
    // Create path number 1 and a Pen for drawing.
    GraphicsPath myPath = new GraphicsPath();
    Pen pathPen = new Pen(Color.get_Black(), 1);

    // Add an Ellipse to the path and Draw it (circle in start
    // position).
    myPath.AddEllipse(20, 20, 100, 100);
    e.get_Graphics().DrawPath(pathPen, myPath);

    // Get the path bounds for Path number 1 and draw them.
    RectangleF boundRect = myPath.GetBounds();

    e.get_Graphics().DrawRectangle(new Pen(Color.get_Red(), 1),
        boundRect.get_X(), boundRect.get_Y(), boundRect.get_Height(),
        boundRect.get_Width());

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

    // Create a new ellipse with a width of 10.
    myPath2.AddEllipse(150, 20, 100, 100);
    myPath2.Widen(pathPen2);
    e.get_Graphics().FillPath(Brushes.get_Black(), myPath2);

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

    // Draw the bounding rectangle.
    e.get_Graphics().DrawRectangle(new Pen(Color.get_Red(), 1),
        boundRect2.get_X(), boundRect2.get_Y(), boundRect2.get_Height(),
        boundRect2.get_Width());

    // Display the rectangle size.
    MessageBox.Show(boundRect2.ToString());
} //GetBoundsExample

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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Visual FoxPro code:      CChalom   |   Edit   |   Show History

Converted code from .NET to GdiPlusX VFP-X Foundation Classes
http://www.codeplex.com/VFPX/Wiki/View.aspx?title=GDIPlusX&referringTitle=Home

 _SCREEN.AddProperty("System", NEWOBJECT("xfcSystem", LOCFILE("system.vcx","vcx"))) 
 WITH _SCREEN.System.Drawing
 * Retrieve the graphics object.
* Initialize the graphics object to be able to draw in the _screen
LOCAL loScreenGfx AS xfcGraphics
loScreenGfx = .Graphics.FromHwnd(_Screen.HWnd)
 * Create path number 1 and a Pen for drawing.
LOCAL loMyPath as xfcGraphicsPath
loMyPath = .Drawing2D.GraphicsPath.New()
 LOCAL loPathPen as xfcPen
loPathPen = .Pen.New(.Color.Black, 1)
 * Add an Ellipse to the path and Draw it (circle in start position).
loMyPath.AddEllipse(20, 20, 100, 100)
loScreenGfx.DrawPath(loPathPen, loMyPath)
 * Get the path bounds for Path number 1 and draw them.
LOCAL loBoundRect as xfcRectangleF
loBoundRect = loMyPath.GetBounds()
 loScreenGfx.DrawRectangle(.Pen.New(.Color.Red, 1), ;
loBoundRect.X, loBoundRect.Y, loBoundRect.Height, loBoundRect.Width)
 * Create a second graphics path and a wider Pen.
LOCAL loMyPath2 as xfcGraphicsPath
loMyPath2 = .Drawing2D.GraphicsPath.New()
 LOCAL loPathPen2 as xfcPen
loPathPen2 = .Pen.New(.Color.Black, 10)
 * Create a new ellipse with a width of 10.
loMyPath2.AddEllipse(150, 20, 100, 100)
loMyPath2.Widen(loPathPen2)
 loScreenGfx.FillPath(.Brushes.Black, loMyPath2)
 * Get the second path bounds.
LOCAL loBoundRect2 as xfcRectangleF
loBoundRect2 = loMyPath2.GetBounds()
 * Show the bounds in a message box.
loScreenGfx.DrawString("Rectangle2 Bounds: " + ;
loBoundRect2.ToString(), .Font.New("Arial", 8), .Brushes.Black, ;
20, 150)
 * Draw the bounding rectangle.
loScreenGfx.DrawRectangle(.Pen.New(.Color.Red, 1), loBoundRect2.X, ;
loBoundRect2.Y, loBoundRect2.Height, loBoundRect2.Width)
 ENDWITH 
RETURN
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker