Wednesday 4 January 2012

image save in asp.net

 where GallaryImage is the Folder and _Fname is the file Path Save in sql server database. when pic up path from sql server databse table Path ........

 string _Fname;
        string _Path;
        if (FileUpload1.HasFile)
        {
            _Fname = FileUpload1.FileName.ToString();
            _Path = "../GalleryImage/" + _Fname;
            //string rep = _Path.Replace("~/", "");
            string rep = "GalleryImage/" + _Fname;
           // FileUpload1.SaveAs(Server.MapPath(_Path));

            string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string targetPath = Server.MapPath(_Path);
            Stream strm = FileUpload1.PostedFile.InputStream;
            var targetFile = targetPath;
            //Based on scalefactor image size will vary
            GenerateThumbnails(0.5, strm, targetFile);


            string ins = "INSERT INTO Admin_Gallery  (Information, Title, Photo) VALUES     ('" + TextBox1.Text.ToString() + "','" + TextBox2.Text.ToString() + "','" + rep.ToString() + "')";
            dut.ExecuteSql(ins);
            Label1.Text = "Successfully Saved";
            TextBox1.Text = "";
            TextBox2.Text = "";
        }
    }
//--------------Image size Trim or reduce size of iMage
  private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)
    {
        using (var image = System.Drawing.Image.FromStream(sourcePath))
        {
            var newWidth = (int)(image.Width * scaleFactor);
            var newHeight = (int)(image.Height * scaleFactor);
            var thumbnailImg = new Bitmap(newWidth, newHeight);
            var thumbGraph = Graphics.FromImage(thumbnailImg);
            thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbGraph.DrawImage(image, imageRectangle);
            thumbnailImg.Save(targetPath, image.RawFormat);
        }
    }

No comments:

Post a Comment