Compartir a través de


Cómo: Crear figuras a partir de líneas, curvas y formas

Para crear una figura, construya un GraphicsPath y, a continuación, llame a métodos, como AddLine y AddCurve, para agregar primitivos a la ruta.

Ejemplo

En los ejemplos de código siguientes se crean rutas de acceso que tienen ilustraciones:

  • En el primer ejemplo, se crea una trayectoria que contiene una sola figura. La figura consta de un solo arco. El arco tiene un ángulo de barrido de –180 grados, que es contrario a las agujas del reloj en el sistema de coordenadas predeterminado.

  • En el segundo ejemplo, se crea una ruta que tiene dos figuras. La primera figura es un arco seguido de una línea. La segunda figura es una línea seguida de una curva seguida de una línea. La primera figura está abierta y la segunda figura está cerrada.

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)

Compilar el código

Los ejemplos anteriores están diseñados para su uso con Windows Forms y requieren PaintEventArgse, que es un parámetro del Paint controlador de eventos.

Consulte también