Whether you’re building a website, a desktop application, or a mobile app, the ability to manipulate and draw on images is a crucial aspect when processing and editing images. To empower developers with advanced yet easy-to-implement drawing features, we introduce Aspose.Imaging.Drawing, a powerful API designed for C# .NET applications, offering a range of image drawing features.
- C# .NET Drawing API
- Creating Graphics using .NET Drawing Library
- Apply Masking to Images
- Apply Filters to Images
- Crop, Rotate, and Resize Images
- Convert Images to Other Formats
C# .NET Drawing Library
Aspose.Imaging.Drawing is a robust API that allows you to process raster and vector images with advanced image processing techniques. With native byte access, the API ensures high processing speed while creating or manipulating images. Using the API, you can draw objects with pixel manipulation and graphics path and convert images from one format to another. Aspose.Imaging.Drawing uses Aspose.Drawing as the default graphics engine, which is a cross-platform 2D graphics library that offers a complete set of features for drawing text, geometries, and images without any external dependencies.
You can download and install Aspose.Imaging.Drawing from NuGet.
PM> NuGet\Install-Package Aspose.Imaging.Drawing
Let’s now take a look at some salient features of this .NET Drawing API and their usage with the help of source code.
Creating Graphics using .NET Drawing Library
Aspose.Imaging.Drawing allows you to create images by drawing different objects such as circles, lines, triangles, squares, and ellipses. Not only the raster images but you can generate vector images using the API. Let’s have a look at how to draw raster and vector images in C# using this .NET drawing API.
Draw Raster Image
Below are the steps to draw a raster image using Aspose.Imaging.Drawing API.
- First, create an image of the desired format using Image.Create() method.
- Then, create an object of Graphics class and initialize it with Image we have created above.
- Clear the surface of the image using Graphics.Clear method.
- Create a new Pen and initialize it with the desired color.
- Draw an ellipse (or any other graphical object) using Graphics.DrawEllipse() method.
- Draw a polygon using Graphics.FillPolygon() method.
- Finally, save the image using Image.Save() method.
The following code snippet shows how to draw an image in C#.
Draw Vector Image
Similar to raster images, you can draw vector graphics, such as SVG, in a few simple steps. Let’s create a vector image using our C# drawing library.
- First, create a MemoryStream object.
- Then, load an SVG image.
- Rasterize SVG to PNG and write the result to a stream.
- Load a PNG image from the stream for further drawing.
- Draw PNG image on existing SVG image.
- Finally, save the results.
The following code snippet shows how to draw a vector image in C#.
Apply Masking to Images
Masking in image editing refers to the technique of selectively hiding or revealing certain portions of an image. It involves creating a mask, which is essentially a grayscale image that defines the transparency or opacity of each pixel in the original image. Image masking is widely practiced for recognition and diagnostics.
Aspose.Image.Drawing provides two methods of applying image masking:
- Manual Masking - It uses a set of ROIs as the mask. The ROIs for each slice are used to define the mask. It needs additional user input.
- Auto Masking - It does not require a lot of input data from the user but may not be so accurate.
Let’s now see how to apply manual and auto masking on images in C# code.
Manual Image Masking
The following code snippet shows how to apply manual masking on an image in C#.
Auto Image Masking
The code snippet below applies auto masking on an image using .NET drawing API.
Apply Filters to Images
Applying filters to images serves various purposes, and people use them for a variety of reasons, both practical and artistic. The most common uses of image filters include color correction, enhancement of image quality, applying special effects, noise reduction, and so on. Aspose.Imaging.Drawing supports several types of filters that you can apply to the images, such as median, motion weiner, gauss weiner, etc.
For demonstration, take a look at the following code that applies the median filter to an image using Aspose.Imaging.Drawing.
Crop, Rotate, and Resize Images
Cropping, rotating, and resizing images are considered as the essential features of image editing applications. They are the most common image processing operations that are used to manipulate digital images. Let’s take a look at how to crop, rotate, and resize images using Aspose.Imaging.Drawing API.
Crop an Image
To crop an image, you need to provide the shift values for all four sides of the image. Based on these values, the image bounds are shifted towards the center of the image. The following C# code snippet crops an image based on the provided shift values.
Rotate an Image
To rotate an image, Aspose.Imaging.Drawing provides RotateFlip method, which not only rotates an image but also flips it (if needed). You can choose the desired rotation and flipping option from RotateFlipType enum. The following code snippet shows how to rotate an image but not flip it in C#.
Resize an Image
Resizing an image using Aspose.Imaging.Drawing is as simple as a pie. Just call the Resize method and pass the new height and width values. The following code snippet shows how to resize an image in C#.
Convert Images to Other Formats
To convert an image using Aspose.Imaging.Drawing API, you only need to load the image and save it in the desired output format. The following code snippet converts a JPG image into WebP, PSD, PDF, and TIFF formats.
Get a Free API License
If you want to evaluate Aspose.Imaging.Drawing API, you can get a free temporary license which lets you use API to its full capacity without any limitations.
Conclusion
In the realm of graphics and image processing, the Aspose.Imaging.Drawing API stands out as a versatile and powerful tool for C# .NET developers. Whether you’re looking to enhance visual elements, correct image distortions, or convert images between formats, this API provides a comprehensive set of features. With Aspose.Imaging.Drawing, image manipulation comes at your fingertips, empowering you to create compelling and visually stunning graphics programmatically.
Let us know about your feedback or queries on our forum.