Procedura: creare figure da linee, curve e forme

Per creare una figura, creare un GraphicsPathoggetto e quindi chiamare metodi, ad esempio AddLine e AddCurve, per aggiungere primitive al percorso.

Esempio

Gli esempi di codice seguenti creano percorsi con figure:

  • Il primo esempio crea un percorso con una singola figura. La figura è costituita da un singolo arco. L'arco ha un angolo di sweep di –180 gradi, che è antiorario nel sistema di coordinate predefinito.

  • Il secondo esempio 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 viene lasciata aperta e la seconda figura viene chiusa.

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

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

Compilazione del codice

Gli esempi precedenti sono progettati per l'uso con Windows Form e richiedono PaintEventArgse, che è un parametro del Paint gestore eventi.

Vedi anche