Partager via


Courbes ouvertes et fermées dans GDI+

L'illustration suivante représente deux courbes dont l'une est ouverte et l'autre fermée.

Courbes ouvertes et fermées

Interface managée pour les courbes

Les courbes fermées ont un intérieur qui peut être rempli à l'aide d'un pinceau. La classe Graphics de GDI+ fournit les méthodes suivantes pour le remplissage de formes fermées et de courbes : FillRectangle, FillEllipse, FillPie, FillPolygon, FillClosedCurve, FillPath et FillRegion. Lorsque vous appelez une de ces méthodes, vous devez lui passer un type de pinceau précis (SolidBrush, HatchBrush, TextureBrush, LinearGradientBrush ou PathGradientBrush) en tant qu'argument.

La méthode FillPie est associée à la méthode DrawArc. De même que la méthode DrawArc dessine une partie du contour d'une ellipse, la méthode FillPie remplit une portion de l'intérieur d'une ellipse. L'exemple suivant dessine un arc et remplit la portion correspondante de l'intérieur de l'ellipse :

        myGraphics.FillPie(mySolidBrush, 0, 0, 140, 70, 0, 120)
        myGraphics.DrawArc(myPen, 0, 0, 140, 70, 0, 120)

myGraphics.FillPie(mySolidBrush, 0, 0, 140, 70, 0, 120);
myGraphics.DrawArc(myPen, 0, 0, 140, 70, 0, 120);

L'illustration suivante représente l'arc et le secteur rempli.

Courbes ouvertes et fermées

La méthode FillClosedCurve est associée à la méthode DrawClosedCurve. Ces deux méthodes ferment automatiquement la courbe en reliant le point de départ et le point d'arrivée. L'exemple suivant dessine une courbe qui passe par les points (0, 0), (60, 20) et (40, 50). Ensuite, la courbe est fermée automatiquement par liaison du point (40, 50) au point de départ (0, 0) et l'intérieur est rempli à l'aide d'une couleur unie.

        Dim myPointArray As Point() = _
           {New Point(0, 0), New Point(60, 20), New Point(40, 50)}
        myGraphics.DrawClosedCurve(myPen, myPointArray)
        myGraphics.FillClosedCurve(mySolidBrush, myPointArray)

     Point[] myPointArray =
{ new Point(0, 0), new Point(60, 20), new Point(40, 50) };
     myGraphics.DrawClosedCurve(myPen, myPointArray);
     myGraphics.FillClosedCurve(mySolidBrush, myPointArray);

La méthode FillPath remplit l'intérieur des différents éléments d'un tracé. Si un tracé comprend un élément qui ne forme pas une courbe fermée ou une forme, la méthode FillPath ferme automatiquement cet élément et le remplit. L'exemple suivant dessine et remplit un tracé constitué d'un arc, d'une spline cardinale, d'une chaîne et d'un secteur.

        Dim mySolidBrush As New SolidBrush(Color.Aqua)
        Dim myGraphicsPath As New GraphicsPath()

        Dim myPointArray As Point() = { _
           New Point(15, 20), _
           New Point(20, 40), _
           New Point(50, 30)}

        Dim myFontFamily As New FontFamily("Times New Roman")
        Dim myPointF As New PointF(50, 20)
        Dim myStringFormat As New StringFormat()

        myGraphicsPath.AddArc(0, 0, 30, 20, -90, 180)
        myGraphicsPath.AddCurve(myPointArray)
        myGraphicsPath.AddString("a string in a path", myFontFamily, _
           0, 24, myPointF, myStringFormat)
        myGraphicsPath.AddPie(230, 10, 40, 40, 40, 110)

        myGraphics.FillPath(mySolidBrush, myGraphicsPath)
        myGraphics.DrawPath(myPen, myGraphicsPath)

     SolidBrush mySolidBrush = new SolidBrush(Color.Aqua);
     GraphicsPath myGraphicsPath = new GraphicsPath();

     Point[] myPointArray = {
new Point(15, 20), 
new Point(20, 40), 
new Point(50, 30)};

     FontFamily myFontFamily = new FontFamily("Times New Roman");
     PointF myPointF = new PointF(50, 20);
     StringFormat myStringFormat = new StringFormat();

     myGraphicsPath.AddArc(0, 0, 30, 20, -90, 180);
     myGraphicsPath.AddCurve(myPointArray);
     myGraphicsPath.AddString("a string in a path", myFontFamily,
        0, 24, myPointF, myStringFormat);
     myGraphicsPath.AddPie(230, 10, 40, 40, 40, 110);

     myGraphics.FillPath(mySolidBrush, myGraphicsPath);
     myGraphics.DrawPath(myPen, myGraphicsPath);

L'illustration suivante représente ce tracé avec et sans le remplissage uni. Notez que la méthode DrawPath ne remplit pas le texte de la chaîne mais l'encadre. La méthode qui permet de peindre l'intérieur des caractères de la chaîne est FillPath.

Chaîne dans un tracé

Voir aussi

Tâches

Comment : créer des objets graphiques pour le dessin

Référence

System.Drawing.Drawing2D.GraphicsPath

System.Drawing.Pen

System.Drawing.Point

Autres ressources

Lignes, courbes et formes

Génération et dessin de tracés