Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Draw an ellipse in C#
To draw an ellipse in C#, you use the DrawEllipse() method from the Graphics class. This method requires a Pen object to define the ellipse's outline and either a Rectangle or coordinate parameters to specify the ellipse's position and size.
Drawing ellipses is commonly done in Windows Forms applications using the Paint event handler or by overriding the OnPaint method.
Syntax
The DrawEllipse() method has several overloads −
graphics.DrawEllipse(pen, rectangle); graphics.DrawEllipse(pen, x, y, width, height);
Parameters
pen − Defines the color, width, and style of the ellipse outline
rectangle − Specifies the bounding rectangle that contains the ellipse
x, y − Upper-left corner coordinates of the bounding rectangle
width, height − Dimensions of the bounding rectangle
Using DrawEllipse() with Rectangle
Example
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Paint += Form1_Paint;
this.Text = "Draw Ellipse Example";
this.Size = new Size(400, 300);
}
private void Form1_Paint(object sender, PaintEventArgs e) {
Graphics g = e.Graphics;
Pen p = new Pen(Color.Red, 3);
Rectangle r = new Rectangle(50, 50, 200, 100);
g.DrawEllipse(p, r);
p.Dispose();
}
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Using DrawEllipse() with Coordinates
Example
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
this.Paint += Form2_Paint;
this.Text = "Multiple Ellipses";
this.Size = new Size(450, 350);
}
private void Form2_Paint(object sender, PaintEventArgs e) {
Graphics g = e.Graphics;
// Red ellipse
Pen redPen = new Pen(Color.Red, 2);
g.DrawEllipse(redPen, 20, 20, 150, 80);
// Blue ellipse
Pen bluePen = new Pen(Color.Blue, 4);
g.DrawEllipse(bluePen, 200, 50, 180, 180);
// Green ellipse with dashed style
Pen greenPen = new Pen(Color.Green, 3);
greenPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawEllipse(greenPen, 100, 180, 220, 100);
redPen.Dispose();
bluePen.Dispose();
greenPen.Dispose();
}
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form2());
}
}
Drawing Filled Ellipses
To create a filled ellipse, use the FillEllipse() method with a Brush object instead of a Pen −
Example
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Form3 : Form {
public Form3() {
InitializeComponent();
this.Paint += Form3_Paint;
this.Text = "Filled Ellipses";
this.Size = new Size(400, 300);
}
private void Form3_Paint(object sender, PaintEventArgs e) {
Graphics g = e.Graphics;
// Solid brush for filled ellipse
SolidBrush solidBrush = new SolidBrush(Color.Orange);
g.FillEllipse(solidBrush, 50, 50, 120, 80);
// Gradient brush for filled ellipse
System.Drawing.Drawing2D.LinearGradientBrush gradientBrush =
new System.Drawing.Drawing2D.LinearGradientBrush(
new Point(200, 100), new Point(320, 180),
Color.Purple, Color.Pink);
g.FillEllipse(gradientBrush, 200, 100, 120, 80);
solidBrush.Dispose();
gradientBrush.Dispose();
}
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form3());
}
}
Conclusion
The DrawEllipse() method in C# provides a straightforward way to draw ellipses using a Graphics object, Pen for styling, and either a Rectangle or coordinate parameters for positioning. Always dispose of Pen and Brush objects to free system resources.
