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 bitmap images and graphics identical to GDI+. Thus, you can create bitmaps in C# from within your .NET applications.
C# .NET API to Create Bitmap - Overview with C# Bitmap Example
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. Let’s have a look at how to create an image from scratch or draw graphics on an existing image using C# with this amazing drawing API.
- C# .NET Drawing API - Installation
- Create a Bitmap Image from Scratch in C#
- Draw Graphics on Existing Bitmap Image in C#
C# .NET Graphics API - Installation
PM> Install-Package Aspose.Drawing
Create a Bitmap Image from Scratch in 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 a bitmap image in C#.
Output Bitmap Image
You can visit the following documentation article for the code samples of drawing other graphical objects:
Draw Graphics on Existing Bitmap Image in 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#.
C# .NET Bitmap API - Get a Free License
You can get a free temporary license to create bitmap images without evaluation limitations.
In this article, I have shown you how to use the basic features of Aspose.Drawing for .NET and create bitmap images from scratch in C#. In addition, you have seen how to add graphics to existing bitmap 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.