Pages

Subscribe:

Sunday, 26 January 2014

how to resize image

  int imgid1 = 0;
 string str = "select isnull(max(id)+1,1) as id from tblImageGalleryAddEdit";

                    SqlCommand cmdnew = new SqlCommand(str, conn);
                    conn.Open();
                    SqlDataReader rd = cmdnew.ExecuteReader();
                    while (rd.Read())
                    {
                        imgid1 = Convert.ToInt32(rd["id"]);
                    }
    conn.Close();
string  imgname = FileUpload1.FileName.Replace(" ", "_").Replace("'", "_");
string targetPath = Server.MapPath("GalleryImage/" + imgid1 + imgname);
 Stream strm = FileUpload1.PostedFile.InputStream;
 var targetFile = targetPath;
GenerateThumbnails(0.5, strm, targetFile);


 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);
        }
    }

0 comments:

Post a Comment