Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET

Program for Optimization and Resizing of an Image

Rate me:
Please Sign up or sign in to vote.
3.70/5 (17 votes)
31 Mar 2010CPOL4 min read 76.9K   2.6K   31  
This article guides about optimization of the size of an image file (in bytes) and resizing its dimensions (in pixels).
using System;
using System.Web.UI.WebControls;

using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

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

    }

    protected void cvImageType_Validate(object sender, ServerValidateEventArgs e)
    {
        if (chkImageType(Path.GetExtension(fpImage.FileName).ToLower())) e.IsValid = true; else e.IsValid = false;
    }

    protected bool chkImageType(string fExt)
    {
        switch (fExt)
        {
            case ".jpg":
                return true;
            case ".gif":
                return true;
            case ".png":
                return true;
            case ".bmp":
                return true;
            case ".tif":
                return true;
            default:
                return false;
        }
    }

    protected void cvImageSize_Validate(object sender, ServerValidateEventArgs e)
    {
        if (fpImage.FileContent.Length <= 10485760) e.IsValid = true; else e.IsValid = false;
    }

    protected void btnResults_OnClick(object sender, EventArgs e)
    {
        if (Page.IsValid && fpImage.HasFile)
        {
            string tmpName = Guid.NewGuid().ToString();
            fpImage.SaveAs(MapPath("~/Original Images/" + tmpName + Path.GetExtension(fpImage.FileName)));
            imgOriginal.ImageUrl = "~/Original Images/" + tmpName + Path.GetExtension(fpImage.FileName);

            Bitmap original_image = new Bitmap(fpImage.FileContent);
            lblOIW.Text = original_image.Width.ToString();
            lblOIH.Text = original_image.Height.ToString();
            lblOIS.Text = fpImage.FileContent.Length.ToString();
            
            OptimizeNResize(original_image, tmpName);
            if(original_image != null) original_image.Dispose();
        }
    }

    protected void OptimizeNResize(Bitmap original_image, string tmpName)
    {
        Bitmap final_image = null;
        Graphics graphic = null;
        int reqW = Int32.Parse(txtWidth.Text);
        int reqH = Int32.Parse(txtHeight.Text);
        final_image = new Bitmap(reqW,reqH);
        graphic = Graphics.FromImage(final_image);
        graphic.FillRectangle(new SolidBrush(Color.Transparent), new Rectangle(0, 0, reqW,reqH));
        graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; /* new way */
        graphic.DrawImage(original_image, 0, 0, reqW, reqH);
        if (graphic != null) graphic.Dispose();
        final_image.Save(MapPath("~/Resultant Images/" + tmpName + Path.GetExtension(fpImage.FileName)));
        imgResult.ImageUrl = "~/Resultant Images/" + tmpName + Path.GetExtension(fpImage.FileName);
        lblRIW.Text = final_image.Width.ToString();
        lblRIH.Text = final_image.Height.ToString();
        if (original_image != null) original_image.Dispose();
        if (final_image != null) final_image.Dispose();

        FileInfo nfi = new FileInfo(MapPath("~/Resultant Images/" + tmpName + Path.GetExtension(fpImage.FileName)));
        lblRIS.Text = nfi.Length.ToString();
        lnkSave1.NavigateUrl = "~/Original Images/" + tmpName + Path.GetExtension(fpImage.FileName);
        lnkSave2.NavigateUrl = "~/Resultant Images/" + tmpName + Path.GetExtension(fpImage.FileName);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Cherisys Technologies
India India
Senior Software Professional with 13+ years of experience in web/desktop applications development.

Comments and Discussions