How to Resize Image While Uploading in C#


Resize Image in This post explains how to resize images in ASP.Net C# during uploading image file. Basically, we are Resizing and processing images as they are uploaded on the server in C#. For we Dotnet Developer in most of our ASP.Net application, we have uploading image facility. Here we face the performance issue while uploading large size images to display for the user. In my project, there is a similar requirement that the user upload images will be resized and renamed internally through server side while uploading. I have a .aspx page which will upload images to the server hard disk from client PC.  And I need to write a program in such a way that it would allow me to resize the image while uploading. This program (C# code)  also can be used to create thumbnail .i.e  calling this function we can resize the uploaded image to a thumbnail size 100 X 100.

Here’s the code for resize image in C# while upload:

 Firstly, we are going to add the following namespaces in our Webform ( Web Application)

HTML Markup: Adding File control and button on webpage.

Here we added an Fileupload control and button, so we can resize the image to a smaller size and then save it to the server.

Code behind: Code to save and resize image uploaded.

Boom !! This is the masterpiece code.

Here’s in below given code we have a  method which have two parameters, retrieve as  image and height, after retrieving image and height  based on it ratio the image gets resized.

Run this code and you will see whenever the user uploads an image then our masterpiece C# code will resize the image to the appropriate size by setting its height or weight. Also, note we are going to maintain image ratio

Here’s full Code to save and resize the image, also we are applied some validation i.e only image file get uploaded by checking file extension .png, .gif, .jpeg, also checking image size limit.

You can also check   how to Preview Image before upload in c# using jQuery , if you want user, to view thumbnail  photos on the client side i.e before uploading to the server.

  You can also check these articles:
  1. Easy way to upload bulk images in c#.
  2. Preview Image before upload it with jQuery in
  3. Profile photo change with drag drop with Dropzone Js in C#.

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!

Get your Printable Copy


  1. I have tried this code but an error that ScaleImage not available in current context.I imported all namespaces which you metioned in example.
    I google many solution regarding this but could not get any solution so that ScaleImage could be available in current context.
    please provide valuable suggestion?

    • Hi Prasunjeet Soni,
      The error msg ‘ScaleImage not available in current context’ means, you have missed ‘ScaleImage’ method in your code. Pls check the article properly method ScaleImage returns the resized IMAGE