June 12, 2013

Make thumbnail image using ASP.Net

In this post, I post an article regarding image's thumbnail. Thumbnails are images which are in small as compared to their actual size or you can say that they are resized images that are small in physical and file size. They are being resized with the width and height attributes of the img tag.  The benefit of thumbnail is that it loads a small size images very fast as compared to actual size because of low file size which is fastly rendered, only happen if the file size is small. There is no real advantage using the image tag to make a thumbnail because the file size is exactly as the full image. The .NET framework includes some extremely useful functionality and collections of namespaces. One of them is the system.drawing namespace which allows the programmer to work with creating image thumbnails.

Now lets start development. For upload images, we require a FileUpload control, Button and two images for displaying actual and thumbnail image as output. After placing controls on page, your webpage looks like this;
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Thumbnail Creation</title>
    <form id="form1" runat="server">
        <asp:FileUpload ID="fuImage" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
        <asp:Image ID="imgNormal" runat="server" />
        <asp:Image ID="imgThumbnail" runat="server" />
And the code behind is:

protected void btnUpload_Click(object sender, EventArgs e) 
    if (fuImage.HasFile) 
        fuImage.SaveAs(MapPath("Images/" + fuImage.FileName));
        System.Drawing.Image img = System.Drawing.Image.FromFile(MapPath("Images/") + fuImage.FileName);

        System.Drawing.Image bmp1 = img.GetThumbnailImage(50, 50, null, IntPtr.Zero);
        bmp1.Save(MapPath("Thumbnail/") + fuImage.FileName);

        System.Drawing.Image bmp2 = img.GetThumbnailImage(100, 100, null, IntPtr.Zero);
        bmp2.Save(MapPath("Thumbnail/") + fuImage.FileName);

        imgNormal.ImageUrl = "Images/" + fuImage.FileName;                
        imgThumbnail.ImageUrl = "Thumbnail/" + fuImage.FileName;

Hope you understand well. 

1 comment:

  1. I really grateful to this blog,because it was very helpful to get the programming coding of development segment of web solutions.
    Web Design Companies | Web Designing Companies