You would have come across various drawing applications (i.e. MS Paint) that allow you to draw graphics and generate images. However, what if you want to create your own drawing utility or integrate drawing features within your web or desktop application? For such scenarios, Aspose offers its .NET Graphics API as Aspose.Drawing for .NET which is a lightweight solution for creating images and graphics identical to GDI+.
Without any dependencies of the native code and libraries, Aspose.Drawing for .NET lets you draw various graphical objects such as lines, arcs, rectangles, polygons, ellipses, etc. as well as use pens and brushes to create 2D graphics using C# or VB.NET. In this article, I’ll demonstrate how to create an image from scratch or draw graphics on an existing image using C# with our drawing API.
- .NET Graphics API - Installation
- Create an Image from Scratch using C#
- Draw Graphics on Existing Images using C#
.NET Graphics API - Installation
PM> Install-Package Aspose.Drawing
Create an Image from Scratch using C#
In this section, I’ll show you how to use the API for creating an image from scratch. For the demonstration, I’ll add only a couple of graphical objects to the image, however, the same code can be extended for adding the other objects. The following are the steps to create an image using Aspose.Drawing for .NET.
- Create an instance of the Bitmap class.
- Create an instance of Graphics class and initialize it with Bitmap’s instance.
- Define a new Pen object and set its properties.
- Use Graphics.DrawArc(Pen, Single, Single, Single, Single, Single, Single) method to draw an arc.
- Use Graphics.DrawEllipse(Pen, Single, Single, Single, Single) method to draw an ellipse.
- Save the drawing into your desired image format using Bitmap.Save(string) method.
The following code sample shows how to create an image from scratch using C#.
You can visit the following documentation article for the code samples of drawing other graphical objects:
Draw Graphics on Existing Images using C#
You may also load an existing image and draw the desired graphical objects on it. Loading and working with an existing image is as simple as creating a new one. The following are the steps to perform this operation.
- Create an instance of Bitmap class and initialize it with the image’s path.
- Draw desired graphics such as line, polygon, etc. in the same way you have done in the previous example.
- Save the updated image using Bitmap.Save(string) method.
The following code sample shows how to draw graphics on an existing image using C#.
In this article, I have shown you how to use the basic features of Aspose.Drawing for .NET and create images from scratch or add graphics on existing images using C#. In order to explore all the features of the API, you can consult the documentation and download the code samples.
For the upcoming posts and tutorials on Aspose.Drawing for .NET, stay tuned and keep visiting Aspose.Drawing Product Family section.