Saturday, April 28, 2012

How To Resize image in asp.net at upload

 System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FleUpPhoto.PostedFile.InputStream);
                    int imageHeight = imageToBeResized.Height;
                    int imageWidth = imageToBeResized.Width;
                    int maxHeight = 240;
                    int maxWidth = 320;
                    imageHeight = (imageHeight * maxWidth) / imageWidth;
                    imageWidth = maxWidth;

                    if (imageHeight > maxHeight)
                    {
                        imageWidth = (imageWidth * maxHeight) / imageHeight;
                        imageHeight = maxHeight;
                    }

                    Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
                    System.IO.MemoryStream stream = new MemoryStream();
                    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    stream.Position = 0;
                    byte[] image = new byte[stream.Length + 1];
                    stream.Read(image, 0, image.Length);

 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["Connect"]);

 SqlCommand storeimage = new SqlCommand("INSERT INTO TablePic" + "(Content, ImgType, Size,imageSeen) " + " values (@image, @imagetype, @imagesize,@imgSeen)", myConnection);
                   
                    storeimage.Parameters.Add("@image", SqlDbType.Binary, image.Length).Value = image;
                    storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value = FleUpPhoto.PostedFile.ContentType;
                    storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = FleUpPhoto.PostedFile.ContentLength;
                    storeimage.Parameters.Add("@imgSeen", SqlDbType.VarChar, 100).Value = "Off";
                       myConnection.Open();
                    storeimage.ExecuteNonQuery();
                    myConnection.Close();

0 comments:

Post a Comment