Curvas abiertas y cerradas en GDI+

Actualización: noviembre 2007

En la siguiente ilustración se muestran dos curvas: una abierta y otra cerrada.

Curvas abiertas y cerradas

Interfaz administrada para curvas

Las curvas cerradas tienen una parte interior y, por lo tanto, pueden rellenarse con un pincel. La clase Graphics de GDI+ proporciona los métodos siguientes para rellenar formas cerradas y curvas: FillRectangle, FillEllipse, FillPie, FillPolygon, FillClosedCurve, FillPath y FillRegion. Siempre que llame a uno de estos métodos debe pasar uno de los tipos de pincel (SolidBrush, HatchBrush, TextureBrush, LinearGradientBrush o PathGradientBrush) como argumento.

El método FillPie está asociado al método DrawArc. Del mismo modo que el método DrawArc dibuja una parte del contorno de una elipse, el método FillPie rellena una parte del interior de una elipse. En el siguiente ejemplo se dibuja un arco y se rellena la parte correspondiente al interior de la elipse:

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

En la siguiente ilustración se muestra el arco y el sector relleno.

Curvas abiertas y cerradas

El método FillClosedCurve está asociado al método DrawClosedCurve. Ambos métodos cierran automáticamente la curva mediante la conexión del extremo con el punto inicial. En el siguiente ejemplo se dibuja una curva que pasa por (0, 0), (60, 20) y (40, 50). Después, la curva se cierra automáticamente mediante la conexión de (40, 50) con el punto inicial (0, 0), y el interior se rellena con un color sólido.

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

El método FillPath rellena el interior de los sectores independientes de un trazado. Si un sector de un trazado no forma una forma o una curva cerrada, el método FillPath cierra este sector del trazado automáticamente antes de rellenarlo. En el siguiente ejemplo se dibuja y se rellena un trazado compuesto por un arco, una curva spline cardinal, una cadena y un sector:

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

En la siguiente ilustración se muestra el trazado con y sin el relleno sólido. Tenga en cuenta que el texto de la cadena tiene contorno, pero no se rellena, con el método DrawPath. El método FillPath es el que pinta el interior de los caracteres de la cadena.

Cadena en un trayecto

Vea también

Tareas

Cómo: Crear objetos Graphics para dibujar

Referencia

System.Drawing.Drawing2D.GraphicsPath

System.Drawing.Pen

System.Drawing.Point

Otros recursos

Líneas, curvas y formas

Crear y dibujar trazados