Simple Method to Resize Image in C#

Resizing image is a important part of software development. It enables us to reduce size dynamically while upload an image. Below is method to resize image in C#

public void ResizeImage(string sSourceImageFileName, string sDestinationImageFileName, int iWidth, int iHeight)
      Size oSize = new Size(iWidth, iHeight);
      Bitmap oBitmap = new Bitmap((Bitmap)Image.FromFile(sSourceImageFileName), oSize);
      oBitmap.Save(sDestinationImageFileName, System.Drawing.Imaging.ImageFormat.Png);

Add the above method in a class Imagecl.cs. Below is the usage of above method in



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="ImageUpload.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Image Resize Example</title>
    <style type="text/css">
      body{font-family:Arial; font-size:12px;}
    <form id="form1" runat="server">
      <table width="100%" cellpadding="2" style="">
        <tr><td colspan="2"><h2>Image Resize Example</h2></td></tr>
          <td width="140px">Image Name:</td>
            <asp:TextBox ID="txtImageName" runat="server"></asp:TextBox>
          <td>Select Image:</td>
          <td><asp:FileUpload ID="fuFile" runat="server" /></td>
        <tr><td colspan="2"><asp:Button ID="btnUpload" runat="server" Text="Upload"  onclick="btnUpload_Click" /></td></tr>


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _ImageUpload : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)

    protected void btnUpload_Click(object sender, EventArgs e)
        string sImagePath = Server.MapPath("~/images/");
        string sExtension = "";
        string sOriginalImage = "";
        string sResizedImage = "";
        string sOriginalImageFullPath = "";
        string sResizedImageFullPath = "";
        if (fuFile.HasFile)
            sExtension = Path.GetExtension(fuFile.FileName);
            sOriginalImage = txtImageName.Text + "_org" + sExtension;
            sResizedImage = txtImageName.Text + "_thumb" + sExtension;
            sOriginalImageFullPath = Path.Combine(sImagePath, sOriginalImage);
            sResizedImageFullPath = Path.Combine(sImagePath, sResizedImage);
            ImageResizecl oImageResizecl = new ImageResizecl();
            oImageResizecl.ResizeImage(sOriginalImageFullPath, sResizedImageFullPath, 60, 75);
            oImageResizecl = null;

The above code will save both original and resized image in images folder. You need to create images folder in your web application directory.

Tags: , , ,

Comments & Responses

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 7 = 12

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>