Tuesday, 26 January 2016

Imge resizing in asp.net.

 <asp:FileUpload ID="filelogoimage" runat="server" />
 <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            if (filelogoimage.HasFile)
            {
                int length = Convert.ToInt32(filelogoimage.PostedFile.ContentLength);
                if (length < 4100000)
                {
                    string strpath = System.IO.Path.GetExtension(filelogoimage.FileName);
                    if (strpath == ".jpg" || strpath == ".jpeg" || strpath == ".gif" || strpath == ".png")
                    {
                        string orgImage = Convert.ToString(Server.MapPath("/VDIS/Logos/"));
                        string comImage = Convert.ToString(Server.MapPath("/VDIS/Logo/"));


                        filelogoimage.SaveAs(orgImage + filelogoimage.FileName);
                        string fullPath = Path.Combine(Server.MapPath("/VDIS/Logos/"), filelogoimage.FileName);

                        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(fullPath);
                        System.Drawing.Image images = FixedSize(originalImage,350,150);

                        images.Save(comImage + Path.GetFileName(filelogoimage.FileName));              
                       

                        if (imageLogo.Visible == false)
                        {
                            imageLogo.Visible = true;
                        }
                        else
                        {
                            imageLogo.Visible = true;
                        }
                        imageLogo.ImageUrl = "~/VDIS/Logo/" + filelogoimage.FileName;
                        ViewState["previousImage"] = null;
                        ViewState["ImageName"] = filelogoimage.FileName;
                    }
                    else
                    {
                        imageLogo.Visible = false;
                        labelDisplay.Text = "Only Allow JPG,gif,png";
                    }
                }
                else
                {
                    imageLogo.Visible = false;
                    labelDisplay.Text = "File is too large for uploading";
                }
            }
        }

        public static System.Drawing.Image FixedSize(System.Drawing.Image fullSizeImg, int Width, int Height)
        {

            int sourceWidth = fullSizeImg.Width;
            int sourceHeight = fullSizeImg.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = System.Convert.ToInt16((Width -
                              (sourceWidth * nPercent)) / 2);
            }
            else
            {
                nPercent = nPercentW;
                destY = System.Convert.ToInt16((Height -
                              (sourceHeight * nPercent)) / 2);
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height,
                              PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(fullSizeImg.HorizontalResolution,
                             fullSizeImg.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.White);
            grPhoto.InterpolationMode = InterpolationMode.Default;
            grPhoto.CompositingQuality = CompositingQuality.HighQuality;
            grPhoto.SmoothingMode = SmoothingMode.HighQuality;

            grPhoto.DrawImage(fullSizeImg,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }

No comments:

Post a Comment