How to Draw Circles in C#
-
Use the
Drawing.Ellipse()
Method to Draw Circles inC#
-
Use the
FillEllipse()
Method to Fill Circles inC#
In this article, we will be looking at how we can draw a circle in C#.
Use the Drawing.Ellipse()
Method to Draw Circles in C#
System.Drawing
doesn’t have an explicit circle draw. We can use the Drawing.Ellipse()
method, which provides the same functionality or create a new FORM
with Windows.FORMS(.NET FRAMEWORK)
to allow us to experiment with interfaces.
Draw Circles in C# Using .NET Framework
Ensure that the Paint
method is called when you have booted into a FORM
. Double click on the form, and the properties will open up.
Switch over to the EVENTS
section.
Inside the EVENTS
section now, scroll down till you find the PAINT
and double click on it to produce the PAINT
function.
Now, we create our ELLIPSE
using the System.Drawing
and then choose the Ellipse option.
e.Graphics.DrawEllipse(new Pen(System.Drawing.Color.Red), new Rectangle(10, 10, 50, 50));
We have chosen a new PEN
in the parameters with the color red. The next parameter tends to draw the RECTANGLE
encapsulating the circle.
Think of it like the size of the circle, with the first two parameters denoting the origin points ( x and y )
and the last two parameters being the size of the x-axis and the y-axis.
Output:
Use the FillEllipse()
Method to Fill Circles in C#
To fill the circle on the above output, we will use the FILLELLIPSE()
function.
e.Graphics.FillEllipse(Brushes.Red, 10, 10, 50, 50);
We defined the same points for the FillEllipse()
function as we did for the Drawing.Ellipse()
to ensure that the correct area is filled. We have chosen the Brushes.Red
brush for it as the first parameter.
Output:
Full Code Snippet:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {}
private void Form1_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawEllipse(new Pen(System.Drawing.Color.Red), new Rectangle(10, 10, 50, 50));
e.Graphics.FillEllipse(Brushes.Red, 10, 10, 50, 50);
}
private void Form1_MouseHover(object sender, EventArgs e) {}
}
}
That is how you draw a circle in C#. We hope you learned this well and can modify it as per your needs.
Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!
GitHub