File upload and thumbnail creation in Asp.Net

InfoPath alternatives for form designing SharePoint
In this article we will discuss how to upload file to a specified path and create a thumbnail for the particular image. You can also check my previous posts on:
To upload an image file we will use FileUpload control which is available in Visual studio toolbax. Also here we will check the size of the image and the extension also.
HTML Code:
<asp:FileUpload ID=”FileUpload1″ runat=”server” Width=”300px” />
<br />
<asp:Button ID=”btnUpload” runat=”server” EnableViewState=”true” Text=”Upload” OnClick=”btnUpload_Click” />
<br />
<asp:Label ID=”lblStatus” runat=”server” Text=”” ForeColor=”Red”></asp:Label>
Here in the above code we have one upload button and one lebel to show any error message.
C#.Net Code:
Button Click Code
            if (FileUpload1.PostedFile != null)
                int fileSize = FileUpload1.PostedFile.ContentLength;
                if (fileSize > 524288)
                    lblStatus.Text = “Please upload file with maximum size 0.5MB”;
                string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                ArrayList arrExtensionList = new ArrayList();
                if (!arrExtensionList.Contains(extension.ToLower()))
                    lblStatus.Text = “Please upload a valid image file. (.png,.jpg,.jpeg,.bmp,.gif,.tif)”;
string ImageName = “SampleImage1.png”;
                FileUpload1.SaveAs(Server.MapPath(“~\\UserPhoto\\”) + ImageName);
                string ThumbnailPath = (Server.MapPath(“~\\UserPhoto\\MyImage.png”);
                using (System.Drawing.Image Img =
                System.Drawing.Image.FromFile(Server.MapPath(“~\\UserPhoto\\”) + ImageName))
                    Size ThumbNailSize = NewImageSize(Img.Height, Img.Width, 150);
                    using (System.Drawing.Image ImgThnail =
                        new Bitmap(Img, ThumbNailSize.Width, ThumbNailSize.Height))
                        ImgThnail.Save(ThumbnailPath, Img.RawFormat);
 //The below line is to delete the original image after thumbnail creation.
                    File.Delete(Server.MapPath(“~\\UserPhoto\\”) + ImageName);
        catch (Exception ex)
The below function is to decide the Size of the thumbnail image.
  public Size NewImageSize(int OriginalHeight, int OriginalWidth, double FormatSize)
        Size NewSize;
        double tempval;
        if (OriginalHeight > FormatSize && OriginalWidth > FormatSize)
            if (OriginalHeight > OriginalWidth)
                tempval = FormatSize / Convert.ToDouble(OriginalHeight);
                tempval = FormatSize / Convert.ToDouble(OriginalWidth);
            NewSize = new Size(Convert.ToInt32(tempval * OriginalWidth), Convert.ToInt32(tempval * OriginalHeight));
            NewSize = new Size(OriginalWidth, OriginalHeight); return NewSize;

Similar SharePoint 2013 Tutorials

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site

View all posts by Bijay Kumar →

Leave a Reply