Compartilhar via


Curvas Abertas e Fechadas no GDI+

A ilustração a seguir mostra duas curvas: uma aberta e outra fechada.

Captura de tela de uma curva aberta e uma curva fechada.

Interface Gerenciada para Curvas

As curvas fechadas têm um interior e, portanto, podem ser preenchidas com um pincel. A classe Graphics em GDI+ fornece os seguintes métodos para preencher formas e curvas fechadas: FillRectangle, FillEllipse, FillPie, FillPolygon, FillClosedCurve, FillPathe FillRegion. Sempre que você chamar um desses métodos, precisará passar um dos tipos de pincel específicos (SolidBrush, HatchBrush, TextureBrush, LinearGradientBrush ou PathGradientBrush) como um argumento.

O método FillPie é um complementar ao método DrawArc. Assim como o método DrawArc desenha uma parte do contorno de uma elipse, o método FillPie preenche uma parte do interior de uma elipse. O exemplo a seguir desenha um arco e preenche a parte correspondente do interior da 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)

A ilustração a seguir mostra o arco e a torta preenchida.

Captura de tela de um gráfico de pizza preenchido.

O método FillClosedCurve é um complementar ao método DrawClosedCurve. Ambos os métodos fecham automaticamente a curva conectando o ponto final ao ponto inicial. O exemplo a seguir desenha uma curva que passa por (0, 0), (60, 20) e (40, 50). Em seguida, a curva é fechada automaticamente conectando-se (40, 50) ao ponto inicial (0, 0) e o interior é preenchido com uma cor sólida.

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

O método FillPath preenche os interiores dos segmentos separados de um caminho. Se uma parte de um caminho não formar uma curva ou forma fechada, o método FillPath fechará automaticamente essa parte do caminho antes de preenchê-la. O seguinte exemplo desenha e preenche um demarcador que consiste em um arco, uma spline cardinal, uma cadeia de caracteres e uma pizza:

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

A ilustração a seguir mostra o demarcador com e sem o preenchimento sólido. Observe que o texto na cadeia de caracteres é descrito, mas não preenchido, pelo método DrawPath. É o método FillPath que pinta os interiores dos caracteres na cadeia de caracteres.

cadeia de caracteres Cadeia de caracteres em um caminho

Consulte também