We often get images with the size that doesn’t meet our requirements, therefore, we have to resize them accordingly. In this article, you will learn how to resize images using C# from within your .NET applications. Moreover, you will learn how to resize raster images and vector images explicitly.
.NET API to Resize Images
To resize images. we will use Aspose.Imaging for .NET. It is a powerful image processing API to manipulate various types of raster and vector images. You can download the API’s DLL or install it via NuGet.
PM> Install-Package Aspose.Imaging
Resize Raster Images using C#
There are two methods to resize raster images (PNG, GIF, JPEG, etc.): simple resizing and resizing using a resize type. In simple resizing, the images are resized by providing the height and width. Whereas, the second method uses different resizing techniques. The following sections demonstrate how to resize an image using each of the above-mentioned methods.
Simple Resizing of an Image
The following are the steps to resize an image in C#.
- First, load the image file using Image class.
- Then, resize image by calling Image.Resize(Int32, Int32) method.
- Finally, save resized image using Image.Save(string) method.
The following code sample shows how to resize an image in .NET applications.
Resize Image using a Resize Type
The ResizeType enum is used to specify resizing techniques that you can use as per your requirements. The complete list of supported resizing types is available here. The following are the steps to resize an image using a particular resize technique.
- First, load the image using Image class.
- Then, resize image by calling Image.Resize(Int32, Int32, ResizeType) method.
- Finally, save resized image using Image.Save(string) method.
The following code sample resizes the image using a particular resize technique.
Resize Image Proportionally
Resizing images by specifying height and width may result in a shrunk or scaled image. Therefore, you have to calculate the aspect ratio manually. However, any miscalculation in aspect ratio may lead to a stretched image. To avoid such issues, you can use proportional resizing. The following are the steps to achieve this.
- First, load the image file using Image class.
- Then, cache image data using Image.CacheData() method.
- Specify new height and width of the image.
- Calculate the proportion values using Image.ResizeWidthProportionally(Int32) and Image.ResizeHeightProportionally(Int32) method.
- Finally, save resized image using Image.Save(string) method.
In case you want to use any particular resize type while resizing images proportionally, use the overload methods Image.ResizeWidthProportionally(Int32, ResizeType) and Image.ResizeHeightProportionally(Int32, ResizeType).
The following code sample shows how to resize an image proportionally using C#.
Resize Vector Images in C#
Aspose.Imaging for .NET also allows you to resize the vector images and save them in a raster image format. The following code sample shows how to resize an SVG image and save the output in PNG format.
Get a Free API License
You can get a free temporary license to use Aspose.Imaging for .NET without evaluation limitations.
Conclusion
In this article, you have learned how to resize images programmatically using C#. Furthermore, the code samples have demonstrated how to perform simple resizing of images or resizing using a particular type. We have also covered how to avoid manual calculation of aspect ratio while resizing images. In addition, resizing of vector images is also discussed at the end. Besides, you can explore more about the .NET image processing API using documentation. Also, you can download the complete package of source code samples from GitHub. In case of any queries, you can contact us on our forum.