Visual C#

GDI+ Shapes: Arcs

An arc is a portion or segment of an ellipse, meaning an arc is a non-complete ellipse. While a pie is a closed shape, an arc is not: it uses only a…

1 min read147 words2 code samples

Introduction#

An arc represents a segment of an ellipse, essentially acting as an incomplete version of that shape. Unlike a pie, which is closed, an arc remains open and consists only of a portion of the ellipse's perimeter. Because it follows the elliptical curve, an arc is defined by how it fits within a bounding rectangle, as illustrated here:

To facilitate drawing arcs, the Graphics class provides four different versions of the DrawArc() method, with the following syntaxes:

C#
public void DrawArc(Pen pen,
                 Rectangle rect,
                 float startAngle, float sweepAngle);
public void DrawArc(Pen pen,
                 RectangleF rect,
                 float startAngle, float sweepAngle);
public void DrawArc(Pen pen,
                 int x, int y, int width, int height,
                 int startAngle, int sweepAngle);
public void DrawArc(Pen pen,
                 float x, float y, float width, float height,
                 float startAngle, float sweepAngle);

The ellipse containing the arc is defined by a Rectangle or RectangleF, or by specifying its x, y coordinates and dimensions. Beyond the bounding box, an arc requires a startAngle, measured clockwise from the x-axis, and a sweepAngle that determines the arc's length starting from the startAngle. These parameters work similarly to those used in the Graphics.Pie() method. Here is an implementation example:

C#
private void Exercise_Paint(object sender, PaintEventArgs e)
{
        Pen penCurrent = new Pen(Color.Red);
        e.Graphics.DrawArc(penCurrent, 20, 20, 200, 150, 225, 200);
}

Home Copyright © 2010-2011 C# Key