Introduction
We have other approaches to resize an image in .NET such as GDI+, WPF, WCI and WebImage method. We implement these approaches using own custom code to resize an image. So, we use third party ‘ImageResizer’ nuget package with managed API to resize an image. It provides on demand image resizing as well.It has a very simple (and powerful) URL API.Thousands of popular websites rely on ImageResizer; some with millions of pageviews each day, like Sierra Trading Post, MSN, and eBay.
What is ImageResizer
- It is an IIS/ASP.NET HttpModule & image server. On-demand image manipulation, delivery, and optimization - with low latency - makes responsive images easy.
- As it’s open source so you can download from Git. The official repository for ImageResizer.
- An image processing library optimized and secured for server-side use.
Getting Started
We create an MVC application in which we upload image from view and shows those images. First and Foremost we create a view model ProfileViewModel as per following code snippet which used to pass images data of a directory from controller to strongly typed view.
- using System.Collections.Generic;
- namespace EFOperation.Models
- {
- public class ProfileViewModel
- {
- public string ProfileImage
- {
- get;
- set;
- }
- public FileInfo[] FileInfoes
- {
- get;
- set;
- }
- }
- }
- using EFOperation.Models;
- using ImageResizer;
- using System.IO;
- using System.Web;
- using System.Web.Mvc;
- namespace EFOperation.Controllers
- {
- public class ProfileController: Controller
- {
- [HttpGet]
- public ActionResult Index()
- {
- ProfileViewModel model = newProfileViewModel();
- model.FileInfoes = newDirectoryInfo(Server.MapPath("~/images")).GetFiles();
- return View(model);
- }
- }
- }
- @model EFOperation.Models.ProfileViewModel
- <div class="row">
- <div class="col-lg-6">
- @using (Html.BeginForm("Index", "Profile", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form" }))
- {
- <h4>Upload Image</h4>
- <hr/>
- <div class="form-group">
- @Html.LabelFor(m =>m.ProfileImage, new { @class = "col-md-2 control-label" })
- <div class="col-md-10">
- <input type="file"name="profileFile"id="profileFile"/>
- </div>
- </div>
- <div class="form-group">
- <div class="col-lg-4">
- <a href="#"class="thumbnail">
- <img id="uploading"src=""alt="uploading image">
- </a>
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit"class="btnbtn-default" value="Submit"/>
- </div>
- </div>
- }
- </div>
- <div class="col-lg-6">
- <h4>Uploaded Images</h4>
- <hr/>
- <div class="row">
- @foreach (var image inModel.FileInfoes)
- {
- <div class="col-lg-3">
- <img src="~/images/@image.Name"alt="@image.Name"class="img-circle">
- </div>
- }
- </div>
- </div>
- </div>
- @section Scripts{
- @Scripts.Render("~/Scripts/profile-index.js")
- }
- (function ($) {
- function ProfileIndex() {
- var $this = this;
- function intialize() {
- $("#profileFile").change(function () {
- readURL(this);
- });
- }
- function readURL(input) {
- if (input.files && input.files[0]) {
- var reader = new FileReader();
- reader.onload = function (e) {
- $('#uploading').attr('src', e.target.result);
- }
- reader.readAsDataURL(input.files[0]);
- }
- }
- $this.init = function () {
- intialize();
- }
- }
- $(function () {
- var self = new ProfileIndex();
- self.init();
- })
- })(jQuery)
- [HttpPost]
- public ActionResult Index(HttpPostedFileBaseprofileFile)
- {
- if (profileFile != null)
- {
- string pic = System.IO.Path.GetFileName(profileFile.FileName);
- string path = System.IO.Path.Combine(Server.MapPath("~/images"), pic);
- profileFile.SaveAs(path);
- }
- return RedirectToAction("Index");
- }

Figure 1: Output of Application
As per figure 1, we see that uploaded images are not showing in appropriate size so that those can fit in UI that’s why we resize an image. We have two options to fix this image UI issue one is image resize at a time of load/render on UI and another is resize image at a time of upload. Both options can be implemented by ‘ImageResizer’ plugin that’s why we install this nugget package.
On Demand Image Processing
As this package provides two options so we choose first on demand image processing option.We install ‘ImageResizer’ nuget package in our MVC application using the Manage NuGet Packages window as shown figure 2 and click on install button. The installation updates both packages.config and Web.config configuration files.

Figure 2: Manage NuGet Packages window
- <configSections>
- <sectionnamesectionname="resizer"type="ImageResizer.ResizerSection,ImageResizer"requirePermission="false" />
- </configSections>
- <modules>
- <removenameremovename="FormsAuthenticationModule" />
- <addnameaddname="ImageResizingModule"type="ImageResizer.InterceptModule"/>
- </modules>
- <div class="col-lg-6">
- <h4>Uploaded Images</h4>
- <hr/>
- <div class="row">
- @foreach (var image inModel.FileInfoes) {
- <div class="col-lg-3">
- <img src="~/images/@image.Name?w=160&h=100" alt="@image.Name" class="img-circle">
- </div>
- }
- </div>
- </div>

Figure 3: Output for on demand image processing
Image Resize by API
- [HttpPost]
- public ActionResult Index(HttpPostedFileBaseprofileFile)
- {
- if (profileFile != null)
- {
- string pic = System.IO.Path.GetFileName(profileFile.FileName);
- string path = System.IO.Path.Combine(Server.MapPath("~/images"), pic);
- profileFile.SaveAs(path);
- ResizeSettings resizeSetting = new ResizeSettings
- {
- Width = 150,
- Height = 100,
- Format = "png"
- };
- ImageBuilder.Current.Build(path, path, resizeSetting);
- }
- return RedirectToAction("Index");
- }
The image/object source may be a physical path (C:..), an app-relative virtual path (~/folder/image.jpg), an Image, Bitmap, Stream, VirtualFile, or HttpPostedFile instance.
The image/object destination may be a Stream instance, a physical path, or an app-relative virtual path.
- new ResizeSettings("maxwidth=100&maxheight=100");
- //or
- new ResizeSettings(Request.QueryString);
- //or
- var r = newResizeSettings();
- r.MaxWidth = 100;
- r.MaxHeight = 100;

Figure 4: Uploading image resize output
It’s a simplest way to reduce image size and improves image loading performance. Most of code is single line code to implement API so it’s easy and simple to integrate in application.
An ASP.NET MVC helpful utility articles collection here: ASP.NET MVC CookBook

Narender kumarPosted Apr 20, 2021, 7:31 AM
Nice Sandeep!!
karaoz OnurPosted Feb 14, 2020, 2:50 PM
Thank you sir
kumar swamyPosted Jan 9, 2018, 5:26 AM
Is this support "bmp" format image
Dr.Ajay KashyapPosted Dec 14, 2016, 7:02 AM
Sandeep Singh Sir When I am resizing image, It is resizing But After resizing Image keeps padding from top and bottom. orignal image dimension is 1920*1080 and i am resizing it in 1000*900 then it showing padding(in white color)
Dr.Ajay KashyapPosted Dec 12, 2016, 2:23 AM
Sandeep Singh Sir Height And Width Working Proper But It Can Not Change image extensions
Vignesh ManiPosted May 21, 2016, 4:37 PM
nice
Kuldeep RathorePosted May 19, 2016, 6:42 AM
but with framework 4.5 ImageResize not working
Prerana TiwariPosted May 19, 2016, 12:17 AM
Nice article
Kuppurasu NagarajPosted May 18, 2016, 1:44 PM
Nice Sharing..
Neeraj KumarPosted May 18, 2016, 1:01 PM
Nice Article...
Sandeep Singh ShekhawatPosted May 18, 2016, 11:52 AM
Glad to see you guys here. Thanks for nice comments :)
Debasis SahaPosted May 18, 2016, 10:21 AM
Good One..
Ketak BhalsingPosted May 18, 2016, 8:19 AM
Very Well Written.....
Bhuvanesh MohankumarPosted May 18, 2016, 3:47 AM
Clear demo :)
Thiruppathi RPosted May 18, 2016, 2:56 AM
Good Article...
Pankaj Kumar ChoudharyPosted May 18, 2016, 2:19 AM
Great Article Sir.....