GDI+ 中的椭圆和弧线

可以使用 Graphics 类的 DrawEllipseDrawArc 方法轻松绘制椭圆形和弧线。

绘制椭圆形

若要绘制椭圆形,需要一个 Graphics 对象和一个 Pen 对象。 Graphics 对象提供 DrawEllipse 方法,而 Pen 对象存储用于呈现椭圆形的线条的属性,例如宽度和颜色。 Pen 对象作为参数之一传递给 DrawEllipse 方法。 传递给 DrawEllipse 方法的其余参数指定椭圆形的边框。 下图显示了一个椭圆形及其边框。

Screenshot of an ellipse surrounded by its bounding rectangle.

以下示例绘制一个椭圆形,边框的宽度为 80,高度为 40,左上角为 (100, 50):

myGraphics.DrawEllipse(myPen, 100, 50, 80, 40);
myGraphics.DrawEllipse(myPen, 100, 50, 80, 40)

DrawEllipseGraphics 类的重载方法,因此可以通过多种方式为其提供参数。 例如,可以构造一个 Rectangle 并将 Rectangle 作为参数传递给 DrawEllipse 方法:

Rectangle myRectangle = new Rectangle(100, 50, 80, 40);
myGraphics.DrawEllipse(myPen, myRectangle);
Dim myRectangle As New Rectangle(100, 50, 80, 40)
myGraphics.DrawEllipse(myPen, myRectangle)

绘制弧线

弧线是椭圆形的一部分。 若要绘制弧线,请调用 Graphics 类的 DrawArc 方法。 DrawArc 方法的参数与 DrawEllipse 方法的参数相同,但 DrawArc 需要起始角度和扫描角度。 以下示例绘制一段弧线,其起始角度为 30 度,扫描角度为 180 度:

myGraphics.DrawArc(myPen, 100, 50, 140, 70, 30, 180);
myGraphics.DrawArc(myPen, 100, 50, 140, 70, 30, 180)

下图显示了弧线、椭圆形和边框。

Screenshot of an ellipse with an arc and its bounding rectangle.

另请参阅