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# Asp.net.

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);
      oBitmap.Dispose();
    }

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

Usage

ImageUpload.aspx

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

ImageUpload.aspx.cs

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);
            fuFile.PostedFile.SaveAs(sOriginalImageFullPath);
            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 *

*


× 9 = 72

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>