Comment : créer des figures à partir de lignes, de courbes et de formes

Pour créer une figure, construisez un GraphicsPath, puis appelez des méthodes, telles que AddLine et AddCurve, pour ajouter des primitives au chemin d’accès.

Exemple

Les exemples de code suivants créent des chemins d’accès qui ont des figures :

  • Le premier exemple crée un chemin d’accès qui a une seule figure. La figure se compose d’un seul arc. L’arc a un angle de balayage de –180 degrés, qui est au sens inverse dans le système de coordonnées par défaut.

  • Le deuxième exemple crée un chemin d’accès qui comporte deux figures. La première figure est un arc suivi d’une ligne. La deuxième figure est une ligne suivie d’une courbe suivie d’une ligne. La première figure est ouverte et la deuxième figure est fermée.

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)

Compilation du code

Les exemples précédents sont conçus pour être utilisés avec Windows Forms et nécessitent PaintEventArgse, qui est un paramètre du Paint gestionnaire d’événements.

Voir aussi