Procedura: definire l'area di visualizzazione utilizzando una regioneHow to: Use Clipping with a Region

Una delle proprietà del Graphics classe è l'area di ritaglio.One of the properties of the Graphics class is the clip region. Tutti i disegni effettuati un determinato Graphics è limitato per l'area di ritaglio di tale oggetto Graphics oggetto.All drawing done by a given Graphics object is restricted to the clip region of that Graphics object. È possibile impostare l'area di ritaglio chiamando il SetClip metodo.You can set the clip region by calling the SetClip method.

EsempioExample

Nell'esempio seguente crea un percorso costituito da un singolo poligono.The following example constructs a path that consists of a single polygon. Nel codice viene quindi creata un'area, in base a tale percorso.Then the code constructs a region, based on that path. L'area viene passata per il SetClip metodo di un Graphics oggetto e quindi due stringhe vengono disegnati.The region is passed to the SetClip method of a Graphics object, and then two strings are drawn.

Nella figura seguente mostra le stringhe tagliate.The following illustration shows the clipped strings.

ClipClip

     // 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));
' 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))

Compilazione del codiceCompiling the Code

L'esempio precedente è progettato per l'uso con Windows Form e richiede PaintEventArgs e, un parametro di PaintEventHandler.The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of PaintEventHandler.

Vedere ancheSee Also

Regioni in GDI+Regions in GDI+
Uso delle regioniUsing Regions