/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/// <summary> /// Save posted data to the db /// </summary> public void Save(object sender, EventArgs e) { if (!Page.IsValid) return; if (Request["ID"] != null) //so editing news { teamMember = GetTeamMember(); } FileUpload fileUpload = ((FileUpload)Page.Master.FindControl("ContentPlaceHolder1").FindControl("fuPhoto")); if (fileUpload.HasFile) { //resize uploaded photo and save using (Bitmap img = ResizeImage(new Bitmap(fuPhoto.PostedFile.InputStream), MemberPhotoWidth, MemberPhotoHeight, FileResizeOptions.MaxWidthAndHeight)) { var filename = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName); var destination = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["TeamMemberPhotosPath"], filename); switch (Path.GetExtension(fuPhoto.FileName).ToLower()) { case ".png": img.Save(destination, System.Drawing.Imaging.ImageFormat.Png); break; default: img.Save(destination, System.Drawing.Imaging.ImageFormat.Jpeg); break; } } } teamMember.Name = txtName.Text; teamMember.Bio = txtBio.Text; teamMember.Modified = DateTime.Now; teamMember.Status = ddStatus.SelectedValue; teamMember.Photo = fuPhoto.FileName; if (Request["ID"] == null) //so adding { db.TeamMembers.InsertOnSubmit(teamMember); } db.SubmitChanges(); Response.Redirect("/Admin/Team", true); } /// <summary> /// Do the actual resize of the image /// </summary> /// <param name="originalImg"></param> /// <param name="widthInPixels"></param> /// <param name="heightInPixels"></param> /// <returns></returns> public static Bitmap DoResize(Bitmap originalImg, int widthInPixels, int heightInPixels) { Bitmap bitmap; try { using (Graphics graphic = Graphics.FromImage(bitmap)) { // Quality properties graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphic.DrawImage(originalImg, 0, 0, widthInPixels, heightInPixels); return bitmap; } } finally { if (originalImg != null) { originalImg.Dispose(); } } } /// <summary> /// Determine how the image will be resized. /// </summary> /// <param name="image"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="resizeOptions"></param> /// <returns></returns> public static Bitmap ResizeImage(Bitmap image, int width, int height, FileResizeOptions resizeOptions) { float f_width; float f_height; float dim; switch (resizeOptions) { case FileResizeOptions.ExactWidthAndHeight: return DoResize(image, width, height); case FileResizeOptions.MaxHeight: f_width = image.Width; f_height = image.Height; if (f_height <= height) return DoResize(image, (int)f_width, (int)f_height); dim = f_width / f_height; width = (int)((float)(height) * dim); return DoResize(image, width, height); case FileResizeOptions.MaxWidth: f_width = image.Width; f_height = image.Height; if (f_width <= width) return DoResize(image, (int)f_width, (int)f_height); dim = f_width / f_height; height = (int)((float)(width) / dim); return DoResize(image, width, height); case FileResizeOptions.MaxWidthAndHeight: int tmpHeight = height; int tmpWidth = width; f_width = image.Width; f_height = image.Height; if (f_width <= width && f_height <= height) return DoResize(image, (int)f_width, (int)f_height); dim = f_width / f_height; // Check if the width is ok if (f_width < width) width = (int)f_width; height = (int)((float)(width) / dim); // The width is too width if (height > tmpHeight) { if (f_height < tmpHeight) height = (int)f_height; else height = tmpHeight; width = (int)((float)(height) * dim); } return DoResize(image, width, height); default: return image; } } }