Procedura: creare figure da linee, curve e forme

Aggiornamento: novembre 2007

Per creare una figura, costruire un oggetto GraphicsPath e quindi chiamare i metodi, ad esempio AddLine e AddCurve, per aggiungere le primitive al percorso.

Esempio

Negli esempi di codice riportati di seguito vengono creati percorsi contenenti figure:

  • Nel primo esempio qui di seguito viene creato un percorso con una singola figura. La figura è composta da un singolo arco. L'arco ha un angolo di scansione di -180 gradi, ovvero in senso antiorario nel sistema di coordinate predefinito.

  • Nel secondo esempio qui di seguito si crea un percorso con due figure. La prima figura è un arco seguito da una linea. La seconda figura è una linea seguita da una curva seguita da una linea. La prima figura resta aperta, la seconda è chiusa.

Dim path As New GraphicsPath()
path.AddArc(175, 50, 50, 50, 0, -180)
e.Graphics.DrawPath(New Pen(Color.FromArgb(128, 255, 0, 0), 4), path)

GraphicsPath path = new GraphicsPath();
path.AddArc(175, 50, 50, 50, 0, -180);
e.Graphics.DrawPath(new Pen(Color.FromArgb(128, 255, 0, 0), 4), path);
' Create an array of points for the curve in the second figure.
Dim points As Point() = { _
   New Point(40, 60), _
   New Point(50, 70), _
   New Point(30, 90)}

Dim path As New GraphicsPath()

path.StartFigure() ' Start the first figure.
path.AddArc(175, 50, 50, 50, 0, -180)
path.AddLine(100, 0, 250, 20)
' First figure is not closed.

path.StartFigure() ' Start the second figure.
path.AddLine(50, 20, 5, 90)
path.AddCurve(points, 3)
path.AddLine(50, 150, 150, 180)
path.CloseFigure() ' Second figure is closed.
e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 2), path)

     // Create an array of points for the curve in the second figure.
     Point[] points = {
new Point(40, 60),
new Point(50, 70),
new Point(30, 90)};

     GraphicsPath path = new GraphicsPath();

     path.StartFigure(); // Start the first figure.
     path.AddArc(175, 50, 50, 50, 0, -180);
     path.AddLine(100, 0, 250, 20);
     // First figure is not closed.

     path.StartFigure(); // Start the second figure.
     path.AddLine(50, 20, 5, 90);
     path.AddCurve(points, 3);
     path.AddLine(50, 150, 150, 180);
     path.CloseFigure(); // Second figure is closed.

     e.Graphics.DrawPath(new Pen(Color.FromArgb(255, 255, 0, 0), 2), path);

Compilazione del codice

Gli esempi precedenti sono progettati per essere utilizzati con Windows Form e richiedono PaintEventArgs e, un parametro del gestore eventi Paint.

Vedere anche

Riferimenti

GraphicsPath

Altre risorse

Costruzione e creazione di percorsi

Utilizzo di un oggetto Pen per creare linee e forme