Home > ASP.Net > Resize Uploaded Image

Resize Uploaded Image


try this example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GDI-UploadImage.aspx.cs" Inherits="GDI_UploadImage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" <a href="http://www.w3.org/TR/html4/strict.dtd&quot;&gt">http://www.w3.org/TR/html4/strict.dtd</a>">
<html lang="en-GB">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>ASP.NET 2.0 [GDI+ Upload Image Example]</title>
    <form id="form2" runat="server">
        <asp:FileUpload ID="fileUpload" runat="server" />
        <asp:Button ID="uploadButton" runat="server" Text="Upload!" OnClick="UploadFile" />
        <asp:Label ID="label" runat="server"></asp:Label>          
        <asp:Image ID="Image1" ImageUrl="" runat="server" />


In code behind :-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO; // this is for the file upload
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

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


    protected void UploadFile(Object s, EventArgs e)
        if (fileUpload.HasFile)
            // Find the fileUpload control

            string filename = Guid.NewGuid().ToString() + Path.GetExtension(fileUpload.FileName);
            // Specify a upload directory
            string directory = Server.MapPath("~/Upload/");
            // Create a bitmap in memory of the content of the fileUpload control
            Bitmap originalBMP = new Bitmap(fileUpload.FileContent);

            // Calculate the new image dimensions
            int origWidth = originalBMP.Width;
            int origHeight = originalBMP.Height;
            int sngRatio = origWidth / origHeight;
            int newWidth = 100;
            //int newHeight = newWidth / sngRatio;
            int newHeight = 100;

            // Check Hieht and width of image
            if (origWidth <= 100 && origHeight <= 100)
                fileUpload.SaveAs(directory + filename);
                // Write a message to inform the user all is OK
                label.Text = "File <b style='color: red;'>" + filename + "</b> uploaded.";

                // Display the image to the user
                Image1.ImageUrl = "~/Upload/" + filename;

                // Create a new bitmap which will hold the previous resized bitmap
                Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);

                // Create a graphic based on the new bitmap
                Graphics oGraphics = Graphics.FromImage(newBMP);

                // Set the properties for the new graphic file
                oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
                oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

                // Draw the new graphic based on the resized bitmap
                oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);

                // Save the new graphic file to the server
                newBMP.Save(directory + "tn_" + filename);

                // Once finished with the bitmap objects, we deallocate them.
                originalBMP = null;
                newBMP = null;
                oGraphics = null;

                // Write a message to inform the user all is OK
                label.Text = "File <b style='color: red;'>" + filename + "</b> uploaded.";

                // Display the image to the user
                Image1.ImageUrl = "~/Upload/tn_" + filename;
            label.Text = "No file uploaded!";

Good Luck

Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: