Translate

> > ASP.NET Image Uploader Class example

ASP.NET Image Uploader Class example

Posted on Thursday, January 17, 2013 | No Comments

  <p align="center">
                        <asp:Image ID="Image1" runat="server" Height="105px"
                            ImageUrl="~/Images/default.gif" Width="135px" /><br />
                        <asp:Label ID="lblUploadStatus" runat="server" Text=""></asp:Label>
                        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                        <asp:LinkButton ID="LinkButton1"
                            runat="server" onclick="LinkButton1_Click">Upload</asp:LinkButton>
                            <br />
                          
                        <asp:Button ID="btnChangeImage" runat="server" Text="Change"
                            onclick="btnChangeImage_Click" />
                    </p>

public class Uploader : System.Web.UI.Page
    {
        public string UploadItemImage(FileUpload fileUpload)
        {
            string filePath = "";
            if (fileUpload.HasFile)
            {
                if (CheckFileType(fileUpload))
                {
                    string postedLogo = fileUpload.PostedFile.FileName.ToString();
                    string fileName = postedLogo.Split(new char[] { '\\' }).Last();
                    MembershipUser mu = Membership.GetUser();
                    string fileExtension = fileUpload.PostedFile.FileName.Split(new char[] { '.' }).Last().ToLower();
                    filePath = "~/ItemImages/" + fileUpload.FileName.Replace(fileName, mu.UserName + "." + fileExtension);


                    string filename = mu.UserName + "." + fileExtension;

                    try
                    {
                        System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(fileUpload.PostedFile.InputStream);


                        if (System.IO.File.Exists(Server.MapPath(filePath)))
                        {
                            System.IO.File.Delete(Server.MapPath(filePath));
                        }

                        ResizeImage(filename, fileUpload);

                        Session["imgPathItem"] = filePath;
                        return filePath;
                    }
                    catch (Exception ex)
                    {
                        Session["ex"] = ex.Message;
                    }
                }
                else
                {

                    return "";

                }
            }
            else
            {
                return "";

            }
            return "";
        }


        private void ResizeImage(string filename,FileUpload fileUpload)
        {

            if (!Directory.Exists(Server.MapPath(@"~/ItemImages")))
                {
                    Directory.CreateDirectory(Server.MapPath(@"~/ItemImages"));
                }
                string directory = Server.MapPath(@"~/ItemImages/") + filename;
                Bitmap originalBMP = new Bitmap(fileUpload.FileContent);
                int origWidth = originalBMP.Width;
                int origHeight = originalBMP.Height;
                int sngRatio = ((int)(origWidth / origHeight) == 0) ? 1 : (origWidth / origHeight);
                int newWidth = 150;
                int newHeight = newWidth / sngRatio;
                Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
                Graphics oGraphics = Graphics.FromImage(newBMP);

                oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

                oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);

                newBMP.Save(directory, ImageFormat.Jpeg);
              
                originalBMP.Dispose();
                newBMP.Dispose();
                oGraphics.Dispose();
           
          
        }

        public bool SaveFinalFile(string fileLoc, string newFileName)
        {
            try
            {
                if (File.Exists(Server.MapPath(fileLoc)))
                {
                    string newFileLocation = Server.MapPath(@"~/ItemImages/") + newFileName + "." + Session["imageExtensition"].ToString();
                    File.Copy(Server.MapPath(fileLoc), newFileLocation,true);
                    Session["imgPathItem"] = @"~/ItemImages/" + newFileName + "." + Session["imageExtensition"].ToString();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
        }

        private bool CheckFileType(FileUpload fileUpload)
        {
            string[] fileTypeList = { "jpeg", "jpg", "gif", "bmp", "png" };
            string fileExtension = fileUpload.PostedFile.FileName.Split(new char[] { '.' }).Last().ToLower();
            bool flag = false;
            for (int i = 0; i < fileTypeList.Count(); i++)
            {
                if (fileExtension == fileTypeList[i])
                {
                    flag = true;
                    Session["imageExtensition"] = fileExtension;
                    break;
                }
            }
            if (flag)
            {
                return true;
            }
            return false;
        }

    }

Leave a Reply

Powered by Blogger.