Graphics.DrawArc() Method in C# with Examples
Last Updated :
04 Aug, 2022
Graphics.DrawArc Method is used to draw an arc representing a portion of an ellipse specified by a pair of coordinates, a width, and a height. There are 4 methods in the overload list of this method as follows:
- DrawArc(Pen,Rectangle,Single,Single)Method
- DrawArc(Pen,RectangleF,Single,Single)Method
- DrawArc(Pen,Int32,Int32,Int32,Int32,Int32,Int32)Method
- DrawArc(Pen,Single,Single,Single,Single,Single,Single)Method)
DrawArc(Pen, Rectangle, Single, Single)
This method is used to draw an arc representing a portion of an ellipse specified by a Rectangle structure.
Syntax: public void DrawArc (System.Drawing.Pen pen, System.Drawing.Rectangle rect, float startAngle, float sweepAngle)
Parameters: pen: Determines the color, width, and style of the arc.
rect: Determines the smallest rectangle in which the ellipse just perfectly fits into.
startAngle: Angle in degrees measured clockwise from the x-axis to the starting point of the arc.
sweepAngle: Angle in degrees measured clockwise from the startAngle parameter to ending point of the arc.
Exception: This method will give ArgumentNullException if the pen is null.
Example:
csharp
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm : Form {
public static void Main()
{
Application.Run( new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true ;
}
protected override void OnPaint(PaintEventArgs e)
{
Pen blackPen = new Pen(Color.Black, 3);
Rectangle rect = new Rectangle(0, 0, 100, 200);
float startAngle = 45.0F;
float sweepAngle = 270.0F;
e.Graphics.DrawArc(blackPen, rect,
startAngle, sweepAngle);
}
}
}
|
Output:
DrawArc(Pen, RectangleF, Single, Single)
This method is used to draw an arc representing a portion of an ellipse specified by a RectangleF structure.
Syntax: public void DrawArc (System.Drawing.Pen pen, System.Drawing.RectangleF rect, float startAngle, float sweepAngle);
Parameters: pen: Determines the color, width, and style of the arc.
rect: Determines the smallest rectangle in which the ellipse just perfectly fits into.
startAngle: Angle in degrees measured clockwise from the x-axis to the starting point of the arc.
sweepAngle: Angle in degrees measured clockwise from the startAngle parameter to ending point of the arc.
Exception: This method will give ArgumentNullException if the pen is null.
Example 1:
csharp
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm : Form {
public static void Main()
{
Application.Run( new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true ;
}
protected override void OnPaint(PaintEventArgs e)
{
Pen blackPen = new Pen(Color.Black, 3);
RectangleF rect = new RectangleF(0.0F,
0.0F, 100.0F, 200.0F);
float startAngle = 45.0F;
float sweepAngle = 270.0F;
e.Graphics.DrawArc(blackPen, rect,
startAngle, sweepAngle);
}
}
}
|
Output:
DrawArc(Pen, Single, Single, Single, Single, Single, Single)
This method is used to draw an arc representing a portion of an ellipse specified by a pair of coordinates, a width, and a height.
Syntax: public void DrawArc (System.Drawing.Pen pen, float x, float y, float width, float height, float startAngle, float sweepAngle); Parameters: pen: Pen determines the color, width, and style of the line.
x: The abscissa of the upper-left corner of the rectangle that defines the ellipse.
y: The ordinate of the upper-left corner of the rectangle that defines the ellipse.
width: Width of the rectangle that defines the ellipse or more specifically we can say the diameter of the ellipse in abscissa.
height: Height of the rectangle that defines the ellipse or more specifically we can say the diameter of the ellipse in ordinate.
startAngle: Angle in degrees measured clockwise from the x-axis to the starting point of the arc.
sweepAngle: Angle in degrees measured clockwise from the startAngle parameter to the ending point of the arc.
Exception: This method will give ArgumentNullException if the pen is null.
Example 1:
csharp
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm : Form {
public static void Main()
{
Application.Run( new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true ;
}
protected override void OnPaint(PaintEventArgs e)
{
Pen blackPen = new Pen(Color.Black, 3);
int x = 0;
int y = 0;
int width = 100;
int height = 200;
int startAngle = 0;
int sweepAngle = 360;
e.Graphics.DrawArc(blackPen, x, y, width,
height, startAngle, sweepAngle);
}
}
}
|
Output:
Example 2:
csharp
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm : Form {
public static void Main()
{
Application.Run( new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true ;
}
protected override void OnPaint(PaintEventArgs e)
{
Pen blackPen = new Pen(Color.Black, 3);
e.Graphics.DrawArc(blackPen, 10,
10, 100, 200, 0, 315);
e.Graphics.DrawLine(blackPen,
60, 110, 110, 110);
e.Graphics.DrawLine(blackPen,
130, 10, 130, 210);
e.Graphics.DrawLine(blackPen,
130, 10, 200, 10);
e.Graphics.DrawLine(blackPen,
130, 110, 170, 110);
e.Graphics.DrawArc(blackPen,
210, 10, 100, 200, 0, 315);
e.Graphics.DrawLine(blackPen,
260, 110, 310, 110);
}
}
}
|
Output:
DrawArc(Pen, Int32, Int32, Int32, Int32, Int32, Int32)
This method is used to draw an arc representing a portion of an ellipse specified by a pair of coordinates, a width, and a height.
Syntax: public void DrawArc (System.Drawing.Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle);
Parameters: pen: Pen determines the color, width, and style of the line.
x: The abscissa of the upper-left corner of the rectangle that defines the ellipse.
y: The ordinate of the upper-left corner of the rectangle that defines the ellipse.
width: Width of the rectangle that defines the ellipse or more specifically we can say the diameter of the ellipse in abscissa.
height: Height of the rectangle that defines the ellipse or more specifically we can say the diameter of the ellipse inordinate.
startAngle: Angle in degrees measured clockwise from the x-axis to the starting point of the arc.
sweepAngle: Angle in degrees measured clockwise from the startAngle parameter to ending point of the arc.
Exception: This method will give ArgumentNullException if the pen is null.
Example:
csharp
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm : Form {
public static void Main()
{
Application.Run( new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true ;
}
protected override void OnPaint(PaintEventArgs e)
{
Pen blackPen = new Pen(Color.Black, 3);
float x = 0.0F;
float y = 0.0F;
float width = 100.0F;
float height = 200.0F;
float startAngle = 0.0F;
float sweepAngle = 360.0F;
e.Graphics.DrawArc(blackPen, x, y, width,
height, startAngle, sweepAngle);
}
}
}
|
Output:
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...