Resize uploaded image on the fly


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Save posted data to the db
  3. /// </summary>
  4. public void Save(object sender, EventArgs e)
  5. {
  6. if (!Page.IsValid) return;
  7.  
  8. if (Request["ID"] != null) //so editing news
  9. {
  10. teamMember = GetTeamMember();
  11. }
  12.  
  13. FileUpload fileUpload = ((FileUpload)Page.Master.FindControl("ContentPlaceHolder1").FindControl("fuPhoto"));
  14. if (fileUpload.HasFile)
  15. {
  16. //resize uploaded photo and save
  17. using (Bitmap img = ResizeImage(new Bitmap(fuPhoto.PostedFile.InputStream), MemberPhotoWidth, MemberPhotoHeight, FileResizeOptions.MaxWidthAndHeight))
  18. {
  19. var filename = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName);
  20. var destination = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["TeamMemberPhotosPath"], filename);
  21.  
  22. switch (Path.GetExtension(fuPhoto.FileName).ToLower())
  23. {
  24. case ".png":
  25. img.Save(destination, System.Drawing.Imaging.ImageFormat.Png);
  26. break;
  27. default:
  28. img.Save(destination, System.Drawing.Imaging.ImageFormat.Jpeg);
  29. break;
  30. }
  31. }
  32. }
  33.  
  34. teamMember.Name = txtName.Text;
  35. teamMember.Bio = txtBio.Text;
  36. teamMember.Modified = DateTime.Now;
  37. teamMember.Status = ddStatus.SelectedValue;
  38. teamMember.Photo = fuPhoto.FileName;
  39.  
  40. if (Request["ID"] == null) //so adding
  41. {
  42. db.TeamMembers.InsertOnSubmit(teamMember);
  43. }
  44.  
  45. db.SubmitChanges();
  46.  
  47. Notification n = new Notification("Save Complete.", ScreenMessageType.Confirmation);
  48. Response.Redirect("/Admin/Team", true);
  49. }
  50.  
  51. /// <summary>
  52. /// Do the actual resize of the image
  53. /// </summary>
  54. /// <param name="originalImg"></param>
  55. /// <param name="widthInPixels"></param>
  56. /// <param name="heightInPixels"></param>
  57. /// <returns></returns>
  58. public static Bitmap DoResize(Bitmap originalImg, int widthInPixels, int heightInPixels)
  59. {
  60. Bitmap bitmap;
  61. try
  62. {
  63. bitmap = new Bitmap(widthInPixels, heightInPixels);
  64. using (Graphics graphic = Graphics.FromImage(bitmap))
  65. {
  66. // Quality properties
  67. graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  68. graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  69. graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
  70. graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  71.  
  72. graphic.DrawImage(originalImg, 0, 0, widthInPixels, heightInPixels);
  73. return bitmap;
  74. }
  75. }
  76. finally
  77. {
  78. if (originalImg != null)
  79. {
  80. originalImg.Dispose();
  81. }
  82. }
  83. }
  84.  
  85. /// <summary>
  86. /// Determine how the image will be resized.
  87. /// </summary>
  88. /// <param name="image"></param>
  89. /// <param name="width"></param>
  90. /// <param name="height"></param>
  91. /// <param name="resizeOptions"></param>
  92. /// <returns></returns>
  93. public static Bitmap ResizeImage(Bitmap image, int width, int height, FileResizeOptions resizeOptions)
  94. {
  95. float f_width;
  96. float f_height;
  97. float dim;
  98. switch (resizeOptions)
  99. {
  100. case FileResizeOptions.ExactWidthAndHeight:
  101. return DoResize(image, width, height);
  102.  
  103. case FileResizeOptions.MaxHeight:
  104. f_width = image.Width;
  105. f_height = image.Height;
  106.  
  107. if (f_height <= height)
  108. return DoResize(image, (int)f_width, (int)f_height);
  109.  
  110. dim = f_width / f_height;
  111. width = (int)((float)(height) * dim);
  112. return DoResize(image, width, height);
  113.  
  114. case FileResizeOptions.MaxWidth:
  115. f_width = image.Width;
  116. f_height = image.Height;
  117.  
  118. if (f_width <= width)
  119. return DoResize(image, (int)f_width, (int)f_height);
  120.  
  121. dim = f_width / f_height;
  122. height = (int)((float)(width) / dim);
  123. return DoResize(image, width, height);
  124.  
  125. case FileResizeOptions.MaxWidthAndHeight:
  126. int tmpHeight = height;
  127. int tmpWidth = width;
  128. f_width = image.Width;
  129. f_height = image.Height;
  130.  
  131. if (f_width <= width && f_height <= height)
  132. return DoResize(image, (int)f_width, (int)f_height);
  133.  
  134. dim = f_width / f_height;
  135.  
  136. // Check if the width is ok
  137. if (f_width < width)
  138. width = (int)f_width;
  139. height = (int)((float)(width) / dim);
  140. // The width is too width
  141. if (height > tmpHeight)
  142. {
  143. if (f_height < tmpHeight)
  144. height = (int)f_height;
  145. else
  146. height = tmpHeight;
  147. width = (int)((float)(height) * dim);
  148. }
  149. return DoResize(image, width, height);
  150. default:
  151. return image;
  152. }
  153. }
  154. }

URL: http://mironabramson.com/blog/post/2007/10/Resize-images-\\\\\\\\\\\\\\\'on-the-fly\\\\\\\\\\\\\\\'-while-uploading.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.