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
- Draw a Line using C#
- Draw an Ellipse using C#
- Drawing an Arc using C#
- Draw a Rectangle using C#
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#.
- First, create an object of BmpOptions class and set bits per pixel using BitsPerPixel property.
- Then, assign StreamSource using Source property.
- Create a new image and initialize it with BmpOptions object and the image’s height and width.
- Create an object of Graphics class and initialize it with Image object.
- Clear the surface of the image with some color using Graphics.Clear() method.
- Draw a line using Graphics.DrawLine(Pen, int, int, int, int) method.
- Generate and save image using Image.Save() method.
The following code sample shows how to draw a line in C#.
The following is the output of the above code sample.
Draw an Ellipse using C#
The following are the steps to draw an ellipse in C#.
- First, create an object of BmpOptions class and set bits per pixel using BitsPerPixel property.
- Then, assign StreamSource using Source property.
- Create a new image and initialize it with BmpOptions object and the image’s height and width.
- Create an object of Graphics class and initialize it with Image object.
- Clear the surface of the image with some color using Graphics.Clear() method.
- Draw an ellipse using Graphics.DrawEllipse(Pen, Rectangle) method.
- Generate and save image using Image.Save() method.
The following code sample shows how to draw an ellipse on an image in C#.
The following is the output of the above code sample.
Draw an Arc using C#
The following are the steps to draw an arc in C#.
- First, create an object of BmpOptions class and set bits per pixel using BitsPerPixel property.
- Then, assign StreamSource using Source property.
- Create a new image and initialize it with BmpOptions object and the image’s height and width.
- Create an object of Graphics class and initialize it with Image object.
- Clear the surface of the image with some color using Graphics.Clear() method.
- Draw arc using Graphics.DrawArc(Pen, float x, float y, float width, float height, float startAngle, float sweepAngle) method.
- Generate and save image using Image.Save() method.
The following code sample shows how to draw an arc on an image in C#.
The following is the output of the above code sample.
Draw an Rectangle using C#
The following are the steps to draw a rectangle in C#.
- First, create an object of BmpOptions class and set bits per pixel using BitsPerPixel property.
- Then, assign StreamSource using Source property.
- Create a new image and initialize it with BmpOptions object and the image’s height and width.
- Create an object of Graphics class and initialize it with Image object.
- Clear the surface of the image with some color using Graphics.Clear() method.
- Draw a rectangle using Graphics.DrawRectangle(Pen, Rectangle) method.
- Generate and save image using Image.Save() method.
The following code sample shows how to draw a rectangle on an image in C#.
The following is the output of the above code sample.
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.