Follow in Twitter & Facebook



Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools



Saturday, July 28, 2012

File upload and thumbnail creation in Asp.Net

Introduction:
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 an useful article on Cookies in asp.net.

Description:
To upload an image file we will use FileUpload asp.net 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
try
        {
            if (FileUpload1.PostedFile != null)
            {
                int fileSize = FileUpload1.PostedFile.ContentLength;
                if (fileSize > 524288)
                {
                    lblStatus.Text = "Please upload file with maximum size 0.5MB";
                    return;
                }
                string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);

                ArrayList arrExtensionList = new ArrayList();
                arrExtensionList.Add(".png");
                arrExtensionList.Add(".jpg");
                arrExtensionList.Add(".jpeg");
                arrExtensionList.Add(".bmp");
                arrExtensionList.Add(".gif");
                arrExtensionList.Add(".tif");

                if (!arrExtensionList.Contains(extension.ToLower()))
                {
                    lblStatus.Text = "Please upload a valid image file. (.png,.jpg,.jpeg,.bmp,.gif,.tif)";
                    return;
                }



                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);
                        ImgThnail.Dispose();
                    }
                    Img.Dispose();

//The below line is to delete the original image after thumbnail creation.
                    File.Delete(Server.MapPath("~\\UserPhoto\\") + ImageName);
                }            
            }
        }
        catch (Exception ex)
        {
            CommonFunctions.SendExceptionMessage(ex.StackTrace);
        }

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);
            else
                tempval = FormatSize / Convert.ToDouble(OriginalWidth);

            NewSize = new Size(Convert.ToInt32(tempval * OriginalWidth), Convert.ToInt32(tempval * OriginalHeight));
        }
        else
            NewSize = new Size(OriginalWidth, OriginalHeight); return NewSize;
    }

You can also check my last article on Validation controls in Asp.net.




0 on: "File upload and thumbnail creation in Asp.Net"