語言

Region.GetBounds(Graphics) 方法

定義

會得到 RectangleF 一個結構,代表一個矩形,將這個 Region 矩形界定在物件的 Graphics 繪圖表面上。

public:
 System::Drawing::RectangleF GetBounds(System::Drawing::Graphics ^ g);
public System.Drawing.RectangleF GetBounds(System.Drawing.Graphics g);
member this.GetBounds : System.Drawing.Graphics -> System.Drawing.RectangleF
Public Function GetBounds (g As Graphics) As RectangleF

參數

g
Graphics

這本書Graphics就是基於這個Region詞。

傳回

一個 RectangleF 結構,代表該矩 Region 形在指定繪圖表面上的界線矩形。

例外狀況

g 是 null。

範例

以下程式碼範例是為 Windows Forms 設計的,並需要 PaintEventArgse,這是 Paint 事件處理程序的參數。 此程式碼會執行下列動作:

  • 建立 a GraphicsPath ,並在上面加上一個橢圓。

  • 用藍色填滿路徑並畫到螢幕上。

  • 建立一個區域,使用該區域。GraphicsPath

  • 結合第二個矩形時,得到該區域的非排斥面積。

  • 取得該區域的邊界矩形,並以紅色畫到螢幕上。

public:
   void GetBoundsExample( PaintEventArgs^ e )
   {
      // Create a GraphicsPath and add an ellipse to it.
      GraphicsPath^ myPath = gcnew GraphicsPath;
      Rectangle ellipseRect = Rectangle(20,20,100,100);
      myPath->AddEllipse( ellipseRect );

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

      // Create a region using the GraphicsPath.
      System::Drawing::Region^ myRegion = gcnew System::Drawing::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 ) );
   }
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));
}
Public Sub GetBoundsExample(ByVal e As PaintEventArgs)

    ' Create a GraphicsPath and add an ellipse to it.
    Dim myPath As New GraphicsPath
    Dim ellipseRect As New Rectangle(20, 20, 100, 100)
    myPath.AddEllipse(ellipseRect)

    ' Fill the path with blue and draw it to the screen.
    Dim myBrush As New SolidBrush(Color.Blue)
    e.Graphics.FillPath(myBrush, myPath)

    ' Create a region using the GraphicsPath.
    Dim myRegion As New [Region](myPath)

    ' Get the bounding rectangle for myRegion and draw it to the
    ' screen in Red.
    Dim boundsRect As RectangleF = myRegion.GetBounds(e.Graphics)
    e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(boundsRect))
End Sub

備註

圖形上下文的當前轉換用於計算繪圖表面內部區域。 根據電流變換,包圍矩形不一定是最小的包圍矩形。

適用於