Image Resize
Image resize during upload?
protected void Button1_Click(object sender, EventArgs e) { newWidth = 100; newHeight = 100; string filename = FileUpload1.FileName.ToString(); System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream); int oldWidth = image.Width; int oldHeight = image.Height; if (oldHeight > oldWidth) { if (((decimal)oldWidth / (decimal)oldHeight) > ((decimal)newWidth / (decimal)newHeight)) { decimal ratio = (decimal)newWidth / oldWidth; newHeight = (int)(oldHeight * ratio); } else { decimal ratio = (decimal)newHeight / oldHeight; newWidth = (int)(oldWidth * ratio); }
Bitmap bitmap = new Bitmap(newWidth, newHeight, PixelFormat.Format48bppRgb); bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics graphics__1 = Graphics.FromImage(bitmap); graphics__1.Clear(Color.White); graphics__1.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics__1.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, oldWidth, oldHeight), GraphicsUnit.Pixel); graphics__1.Dispose();
bitmap.Save(Server.MapPath("image/") + filename, ImageFormat.Jpeg); } else { Response.Write("select valid passport size image"); } |