共用方式為


HOW TO:使用區域的裁剪

更新:2007 年 11 月

Graphics 類別的其中一個屬性就是裁剪區域 (Clip Region)。由指定 Graphics 物件進行的所有繪製工作都限定只能在該 Graphics 物件的裁剪區域中。您可以藉由呼叫 SetClip 方法來設定裁剪區域。

範例

下列範例會建構只包含單一多邊形的路徑。然後程式碼會根據這個路徑來建構區域。這個區域會傳遞至 Graphics 物件的 SetClip 方法,然後繪製兩個字串。

下圖顯示的是裁剪的字串。

裁剪

' Create a path that consists of a single polygon.
Dim polyPoints As Point() = { _
   New Point(10, 10), _
   New Point(150, 10), _
   New Point(100, 75), _
   New Point(100, 150)}
Dim path As New GraphicsPath()
path.AddPolygon(polyPoints)

' Construct a region based on the path.
Dim [region] As New [Region](path)

' Draw the outline of the region.
Dim pen As Pen = Pens.Black
e.Graphics.DrawPath(pen, path)

' Set the clipping region of the Graphics object.
e.Graphics.SetClip([region], CombineMode.Replace)

' Draw some clipped strings.
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
   fontFamily, _
   36, _
   FontStyle.Bold, _
   GraphicsUnit.Pixel)
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 255, 0, 0))

e.Graphics.DrawString( _
   "A Clipping Region", _
   font, _
   solidBrush, _
   New PointF(15, 25))

e.Graphics.DrawString( _
   "A Clipping Region", _
   font, _
   solidBrush, _
   New PointF(15, 68))

     // Create a path that consists of a single polygon.
     Point[] polyPoints = {
new Point(10, 10),
new Point(150, 10), 
new Point(100, 75),
new Point(100, 150)};
     GraphicsPath path = new GraphicsPath();
     path.AddPolygon(polyPoints);

     // Construct a region based on the path.
     Region region = new Region(path);

     // Draw the outline of the region.
     Pen pen = Pens.Black;
     e.Graphics.DrawPath(pen, path);

     // Set the clipping region of the Graphics object.
     e.Graphics.SetClip(region, CombineMode.Replace);

     // Draw some clipped strings.
     FontFamily fontFamily = new FontFamily("Arial");
     Font font = new Font(
        fontFamily,
        36, FontStyle.Bold,
        GraphicsUnit.Pixel);
     SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 255, 0, 0));

     e.Graphics.DrawString(
        "A Clipping Region",
        font, solidBrush,
        new PointF(15, 25));

     e.Graphics.DrawString(
        "A Clipping Region",
        font,
        solidBrush,
        new PointF(15, 68));

編譯程式碼

上述範例是專為與 Windows Form 搭配使用而設計的,而且它需要 PaintEventArgs e (即 PaintEventHandler 的參數)。

請參閱

概念

GDI+ 中的區域

其他資源

使用區域