
Image resizing is a common operation in image processing, useful in various scenarios such as preparing images for web publishing, thumbnails, or reducing file sizes. In this article, we will explore how to resize images in C#. You will learn how to resize raster images like JPG or PNG, perform proportional resizing, and handle vector images such as SVGs with precision.
Why Resize Images in C#?
Resizing images programmatically gives you the ability to automate and scale image processing tasks without manual intervention. Here are some reasons why this is necessary:
- It helps optimize image sizes to reduce loading times and bandwidth usage in web apps.
- Ensures consistent display across various platforms and screen sizes.
- Helps generate image previews and thumbnails dynamically.
- Allows for automated image adjustments in workflows like document generation, archiving, or publishing.
Resize Images - C# API Installation
You can install latest stable version of Conholdate.Total for .NET using the following NuGet installation command in your system:
PM> NuGet\Install-Package Conholdate.Total
Resize Images in C#
Let’s start with basic resizing of raster images like JPGs using fixed dimensions in C#. Simply follow the steps below:
- Load a JPG image from file using Image.Load().
- Call the Resize() method to set the width and height to 300 pixels each.
- Save the resized image using image.Save().
The sample code below demonstrates image resizing in C#:
Resize Images Proportionally in C#
This section demonstrates how to resize a PNG image while maintaining its original aspect ratio in C#.
- Load the PNG image using Image.Load().
- Check if the image is cached; if not, call CacheData() for performance.
- Divide the current width and height by 2 to calculate new proportional dimensions.
- Use ResizeWidthProportionally() and ResizeHeightProportionally() to maintain the aspect ratio.
- Save the resized image with image.Save() method.
The following code snippet shows proportionate image resizing in C#:
Resize Vector Images in C#
This example illustrates how to resize a vector image (SVG) in C#::
- Load an SVG vector image using Image.Load().
- Multiply the current width and height by scaling factors (10 and 15).
- Call Resize() with the new width and height.
- Save the resized image as PNG using image.Save() with PngOptions.
- Specify the SvgRasterizationOptions to ensure proper rendering of the vector image.
The code sample below explains how to resize vector images in C#:
Free Evaluation License
You can evaluate these as well as many other features offered by the API by getting a free temporary license.
Summing Up
Image resizing is an essential function for applications that manage, display, or export visual content. With Conholdate.Total for .NET, resizing images in C# becomes efficient and developer-friendly. It supports a variety of formats and provides fine control over resizing behavior, whether you’re working with fixed dimensions, maintaining aspect ratios, or converting vector images. However, if you have any questions then please feel free to get in touch with us at forum.