Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Program for Optimization and Resizing of an Image

, 31 Mar 2010
This article guides about optimization of the size of an image file (in bytes) and resizing its dimensions (in pixels).
ImageOptimizationnResize.zip
ImageOptimizationnResize
Original Images
readme.docx
Resultant Images
Snapshot.jpg
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)

About the Author

Mohd Arshad (Sam)
Software Developer (Senior) Cherisys Technologies, WebStreet.in
India India

Software professional with demonstrated strength in windows-based and web-based software development. Have 4 years of experience with the full software development lifecycle including requirements, design, development, testing/QA, deployment, training & support. Have 1 year experience managing groups, planning and executing implementations. Practical working knowledge of all aspects of IT. Possess strong insight into practical business considerations.



www.cherisys.com
www.webstreet.in
www.codevdo.com
www.bizkut.com
Follow on   Twitter

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 31 Mar 2010
Article Copyright 2010 by Mohd Arshad (Sam)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid