Resize & crop images using ImageResizer in Asp.Net MVC

How to resize image using ImageResizer library. Here are some simple steps to follow to resize image using ImageResizer.dll library in Asp.Net MVC application.

ImageResizer.png, Asp.Net, Asp.Net, ImageResizer,

Steps to follow

  1. Download & install the ImageResizer Nuget package.
  2. Create function ImageResize in Asp.Net.
  3. Add settings in web.config
  4. Save & resize image


1. Download & install the ImageResizer Nuget package

ImageResizer is a good library to crop & resize images. ImageResizer has a very simple (and powerful) URL API.

An IIS/ASP.NET HttpModule & image server. On-demand image manipulation, delivery, and optimization with low latency makes responsive images easy. An image processing library optimized and secured for server-side use

To download nuget package open visual studio, go to Solution explorer right click add reference and select Manage Nuget package. Search for ImageResizer and click on install.

 Addnugetpackage.png, Asp.Net, Asp.Net, ImageResizer,

InstallImageResizer.png, Asp.Net, Asp.Net, ImageResizer,


2. Create function ImageResize in Asp.Net.

Write a method in C# to resize image using ImageResizer.dll lib. First add reference to ImageResizer.dll in the project and open the cs file and on the top add ImageResizer.dll.

using ImageResizer;

//ImageResize function return byte array
public byte[] ImageResize(Stream imageStream, bool thumb)
 Dictionary<string, string> versions = new Dictionary<string, string>();
 //Define the versions to generate
 versions.Add("_thumb", "width=100&height=100&crop=auto&format=jpg"); //Crop to square thumbnail
 versions.Add("_medium", "maxwidth=200&maxheight=200&format=jpg"); //Fit inside 400x400 area, jpeg
 versions.Add("_large", "format=jpg&mode=max&quality=50"); //Fit inside 1900x1200 area

if (thumb)
 using (var thumbStream = new MemoryStream())
 ImageBuilder.Current.Build(imageStream, thumbStream, new ResizeSettings(versions["_medium"]));
 return thumbStream.ToArray();
 using (var originalStream = new MemoryStream())
 ImageBuilder.Current.Build(imageStream, originalStream, new ResizeSettings(versions["_large"]));
 return originalStream.ToArray();

I have create a common method ImageResize() which resizes image to a thumbnail or large image.

We have set three configurations -

  • thumbnail - "width=100&height=100&crop=auto&format=jpg"
  • medium - "maxwidth=200&maxheight=200&format=jpg"
  • large - "format=jpg&mode=max&quality=50"

The method will generate thumbnail or large image as we boolean value thumb is passed. For thumbnail images I just wanted to have medium images so to convert image into thumbnail I have used medium settings with sizes 200 x 200.


3. Add settings in web.config

Also to use larger images with height & width greater than 3200 specify totalHeight & totalWidth in web.config. As default max sizes for image with ImageResizer is 3200px.

<sizelimits imageWidth="0" imageHeight="0" totalWidth="6000"
totalHeight="4000" totalBehavior="throwexception" />

Also add section in configsections:

<section name="resizer" type="ImageResizer.ResizerSection,ImageResizer" requirePermission="false" />


4. Save & resize image


var httpPostedFile = System.Web.HttpContext.Current.Request.Files["file"];
byte[] content = ImageResize(httpPostedFile.InputStream);

Get the httpPostedFile and pass the InputStream to ImageResize() function which resizes image returning a byte array which we can save to our server.

Let us run and see the output. I have taken LightHouse.jpg as example

Original image -
Dimension – 1024 X 768
Size –548 kb

Output file
Thumb image -
Dimension – 200 X 200
Size – 8.60 kb

 ThumbImage.png, Asp.Net, Asp.Net, ImageResizer,

Large Image -
Dimension – 1024 X 768
Size – 60.07 kb

LargeImage.png, Asp.Net, Asp.Net, ImageResizer,

Back to top