Partager via


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

Mise à jour : novembre 2007

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 comportent des figures :

  • Le premier exemple crée un tracé constitué d'une seule figure. Celle-ci comprend un seul arc. L'arc a un angle de balayage de –180 degrés qui va dans le sens inverse des aiguilles d'une montre dans le système de coordonnées par défaut.

  • Le deuxième exemple crée un tracé constitué de deux figures. La première représente un arc suivi d'une ligne. La seconde représente une ligne suivie d'une courbe elle‑même suivie d'une ligne. La première figure reste ouverte et la seconde est fermée.

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

Compilation du code

Les exemples précédents sont destinés à une utilisation avec Windows Forms et nécessitent PaintEventArgs e, qui est un paramètre du gestionnaire d'événements Paint.

Voir aussi

Référence

GraphicsPath

Autres ressources

Génération et dessin de tracés

Utilisation d'un stylet pour dessiner des lignes et des formes