Image Thumbnail Example in Asp .Net

In this Asp .net tutorial we will learn how to upload images and create thumbnails. Thumbnails are very use full when we want to create Image galleries, Image slide shows etc as they are of small size and thus increase the website speed and also saves bandwidth. For this tutorial we will use FileUpload control to upload images. 

 

Step1: Create a new website.

 

Step2: Create two folders in website.

I have created folder named "Image" to store images and "ImageThumbnail" to store image thumbnails.
Image-Thumbnail-Example-In-Asp-net
Image-Thumbnail-Example-in-Asp-net
Image-Thumbnail-Example-in-Asp-Net

 

Step3: Add following code to your aspx page.

 


 

 

This code will create FileUpload control to select images. A button control to upload images and generat thumbnails on click. An image control to display generated thumbnail.

 

Step4: Add following code to button click event.

 

 protected void btnUpload_Click(object sender, EventArgs e)
    {
        myFileUpload.PostedFile.SaveAs(Server.MapPath("Image") +"/"+ myFileUpload.PostedFile.FileName);

        //===== Generate thumbnail
        GenerateThumbNail(myFileUpload.PostedFile.FileName, "ImageThumbnail", 100);

        //---- Show image in ImageControl.
        myImage.ImageUrl="ImageThumbnail"+"/"+myFileUpload.PostedFile.FileName;
        myImage.Visible=true;
    }

 


Step5: Thumbnail generator code. 

Add this code to your aspx.cs page. Add namespace using System.Drawing;

 

    public string GenerateThumbNail(string imageName, string thumbNailFolder, int width)
    {
        System.Drawing.Image image =
      System.Drawing.Image.FromFile(Server.MapPath("Image") + "/" + imageName.ToString());
        int srcWidth = image.Width;
        int srcHeight = image.Height;
        int thumbWidth = width;
        int thumbHeight;
        Bitmap bmp;
        if (srcHeight > srcWidth)
        {
            thumbHeight = (srcHeight / srcWidth) * thumbWidth;
            bmp = new Bitmap(thumbWidth, thumbHeight);
        }
        else
        {
            thumbHeight = thumbWidth;
            thumbWidth = (srcWidth / srcHeight) * thumbHeight;
            bmp = new Bitmap(thumbWidth, thumbHeight);
        }

        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        System.Drawing.Rectangle rectDestination =
               new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
        string aa = Server.MapPath("~/" + thumbNailFolder + "/" + imageName);

        bmp.Save(HttpContext.Current.Server.MapPath("~/" + thumbNailFolder + "/" + imageName));
        bmp.Dispose();
        image.Dispose();
        return aa;

    }

 

 

Final Output:

Image-Thumbnail-Example-In-Asp-net


Image-Thumbnail-Example-In-Asp-net

 
We are nothing without our users ! You can help us offer even more high quality content. Please share our page !
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
5 + 3 =
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2019 CodingFusion
75 Important SQL Server queries you should know