Draw Shapes in C#

It is often required to create different graphical objects by drawing shapes including circles, lines, rectangles, etc. These shapes could also be used for the annotation of the images. In this article, you will learn how to draw different shapes programmatically in C#. We will demonstrate how to draw lines, ellipses, arcs, and rectangles and generate their images.

C# API to Draw Shapes - Free Download

To draw different types of shapes, we will use Aspose.Imaging for .NET. It is an amazing image editing API that provides a wide range of features to manipulate images and create drawings. You can either download the API or install it from NuGet.

PM> Install-Package Aspose.Imaging

Draw a Line using C#

The following are the steps to draw a line in C#.

The following code sample shows how to draw a line in C#.

// Create BmpOptions
BmpOptions bmpCreateOptions = new BmpOptions();
bmpCreateOptions.BitsPerPixel = 32;
// Define the source property for the instance of BmpOptions
bmpCreateOptions.Source = new StreamSource();
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
Image image = Image.Create(bmpCreateOptions, 500, 500);
// Create and initialize an instance of Graphics class
Graphics graphic = new Graphics(image);
// Clear the image surface with White color
graphic.Clear(Color.White);
// Draw a dotted line by specifying the Pen object having blue color and
// co-ordinate Points
graphic.DrawLine(new Pen(Color.Blue, 3), 18, 18, 200, 200);
graphic.DrawLine(new Pen(Color.Blue, 3), 18, 200, 200, 18);
// Draw a continuous line by specifying the Pen object having Solid
// Brush with red color and two point structures
graphic.DrawLine(new Pen(new SolidBrush(Color.Red), 3),
new Point(18, 18), new Point(18, 200));
// Draw a continuous line by specifying the Pen object having Solid
// Brush with white color and two point structures
graphic.DrawLine(new Pen(new SolidBrush(Color.Orange), 3),
new Point(200, 18), new Point(18, 18));
// Save all changes
image.Save("draw_lines.bmp");
view raw draw-line.cs hosted with ❤ by GitHub

The following is the output of the above code sample.

draw line in C#

Draw an Ellipse using C#

The following are the steps to draw an ellipse in C#.

The following code sample shows how to draw an ellipse on an image in C#.

// Create BmpOptions
BmpOptions bmpCreateOptions = new BmpOptions();
bmpCreateOptions.BitsPerPixel = 32;
// Define the source property for the instance of BmpOptions
bmpCreateOptions.Source = new StreamSource();
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
Image image = Image.Create(bmpCreateOptions, 500, 500);
// Create and initialize an instance of Graphics class
Graphics graphic = new Graphics(image);
// Clear the image surface with White color
graphic.Clear(Color.White);
// Draw a dotted ellipse shape by specifying the Pen object having red
// color and a surrounding Rectangle
graphic.DrawEllipse(new Pen(Color.Red, 3), new Rectangle(60, 40, 70, 120));
// Draw a continuous ellipse shape by specifying the Pen object having
// solid brush with blue color and a surrounding Rectangle
graphic.DrawEllipse(new Pen(new SolidBrush(Color.Blue), 3),
new Rectangle(40, 60, 120, 70));
// Save all changes
image.Save("draw_ellipse.bmp");
view raw draw-ellipse.cs hosted with ❤ by GitHub

The following is the output of the above code sample.

draw ellipse in C#

Draw an Arc using C#

The following are the steps to draw an arc in C#.

The following code sample shows how to draw an arc on an image in C#.

// Create BmpOptions
BmpOptions bmpCreateOptions = new BmpOptions();
bmpCreateOptions.BitsPerPixel = 32;
// Define the source property for the instance of BmpOptions
bmpCreateOptions.Source = new StreamSource();
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
Image image = Image.Create(bmpCreateOptions, 500, 500);
// Create and initialize an instance of Graphics class
Graphics graphic = new Graphics(image);
// Clear the image surface with White color
graphic.Clear(Color.White);
// Draw a dotted arc shape by specifying the Pen object having red black
// color and coordinates, height, width, start & end angles
int width = 200;
int height = 300;
int startAngle = 45;
int sweepAngle = 270;
// Draw arc to screen
graphic.DrawArc(new Pen(Color.Black, 3), 0, 0, width, height, startAngle, sweepAngle);
// Save all changes
image.Save("draw_arc.bmp");
view raw draw-arc.cs hosted with ❤ by GitHub

The following is the output of the above code sample.

draw arc in C#

Draw an Rectangle using C#

The following are the steps to draw a rectangle in C#.

The following code sample shows how to draw a rectangle on an image in C#.

// Create BmpOptions
BmpOptions bmpCreateOptions = new BmpOptions();
bmpCreateOptions.BitsPerPixel = 32;
// Define the source property for the instance of BmpOptions
bmpCreateOptions.Source = new StreamSource();
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
Image image = Image.Create(bmpCreateOptions, 500, 500);
// Create and initialize an instance of Graphics class
Graphics graphic = new Graphics(image);
// Clear the image surface with White color
graphic.Clear(Color.White);
// Draw a dotted rectangle shape by specifying the Pen object having red
// color and a rectangle structure
graphic.DrawRectangle(new Pen(Color.Red, 3),
new Rectangle(60, 40, 70, 120));
// Draw a continuous rectangle shape by specifying the Pen object having
// solid brush with blue color and a rectangle structure
graphic.DrawRectangle(new Pen(new SolidBrush(Color.Blue), 3),
new Rectangle(40, 60, 120, 70));
// Save all changes
image.Save("draw_reactangle.bmp");

The following is the output of the above code sample.

draw rectangle in C#

C# .NET Image Drawing API - Get a Free License

You can get a free temporary license and draw shapes without evaluation limitations.

Conclusion

In this article, you have learned how to draw shapes in C#. We have covered how to draw lines, ellipses, arcs, and rectangles on images programmatically. You can easily integrate the provided code samples into your C# applications.

Read More

You can explore more about the .NET image processing API using documentation. Also, you can share your queries with us via our forum.

See Also