Draw Circle in C#

This topic explains the details to draw circle in C#. It includes the system configuration, stepwise algorithm, and a running code snippet to develop a circle drawer in C#. Moreover, the circle drawing code can be customized further to satisfy your requirements.

Steps to Draw Circle in C#

  1. Configure the environment to use Aspose.Drawing to draw circles
  2. Create an instance of the Bitmap class while passing the dimensions of the image and the pixel format
  3. Initiate an object of the Graphics class using the Bitmap class instance
  4. Invoke the DrawEllipse method while initiating a Pen class instance, and the size of the circle
  5. Write the generated image file

The steps above outline the algorithm to draw a perfect circle in C#. Initiate the process by creating a bitmap of your required dimensions. Subsequently, draw the circle while specifying different characteristics for the shape before writing the output image.

Code for Circle Drawing in C#

// Create bitmap and load into graphics object
Aspose.Drawing.Bitmap BitmapImage = new Aspose.Drawing.Bitmap(512, 512,
Aspose.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Aspose.Drawing.Graphics GraphicsFromImage =
Aspose.Drawing.Graphics.FromImage(BitmapImage);
// Create a pen tool and draw circle
Aspose.Drawing.Pen PenTool = new Aspose.Drawing.Pen(Aspose.Drawing.Color.Green, 3);
GraphicsFromImage.DrawEllipse(PenTool, 20, 20, 400, 400);
// Save output PNG file
BitmapImage.Save("Circle.png");

This brief sample code demonstrates how to make a circle in C#. However, you can improvise it to make different changes, like drawing the circle while loading a source image or creating it from scratch. Likewise, you can modify the line color, line style, fill type, circle size, etc., depending on your needs.

This tutorial has covered circle drawing in C#. Besides, you can draw several other shapes, too, like drawing a Pentagon; you may visit the guide to Draw Pentagon in C#.