C# image processing, useful for resizing images and making thumbnails


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

Just call "ProcessImageAndItsThumbnail" with your newly uploaded image's URL, and it will resize it for you, and make a thumbnail as well. Just remember to set the sizes you want for these new images.


Copy this code and paste it in your HTML
  1. protected void ProcessImageAndItsThumbnail(string url)
  2. {
  3. //make memory streams to work with the image bytes
  4. MemoryStream imageStream = new MemoryStream();
  5. MemoryStream thumbStream = new MemoryStream();
  6.  
  7. //using, so resources get disposed automagically
  8. //using the uploaded image (which might be huge)
  9. using (System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(url)))
  10. {
  11. //for the new resized image
  12. System.Drawing.Image newImage;
  13.  
  14. //if the size is greater than 600 x 450, scale it down
  15. if ((img.Size.Width > 600) && (img.Size.Height > 450))
  16. {
  17. newImage = img.GetThumbnailImage(600, 450, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), System.IntPtr.Zero);
  18. }
  19. else
  20. newImage = img;
  21.  
  22. //and then create a thumbnail too
  23. System.Drawing.Image thumb = img.GetThumbnailImage(200, 150, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), System.IntPtr.Zero);
  24.  
  25. //for the new image
  26. Graphics g = Graphics.FromImage(newImage);
  27. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
  28. g.DrawImage(img, 0, 0, newImage.Width, newImage.Height);
  29.  
  30. //for the thumbnail
  31. Graphics gT = Graphics.FromImage(thumb);
  32. gT.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
  33. gT.DrawImage(img, 0, 0, thumb.Width, thumb.Height);
  34.  
  35. //put the images into the memory streams
  36. newImage.Save(imageStream, ImageFormat.Jpeg);
  37. thumb.Save(thumbStream, ImageFormat.Jpeg);
  38. }
  39.  
  40.  
  41. //then, using a filestream, we write the file back to the same location, for the scaled down file
  42. int bufferSize = 32;
  43. byte[] buffer = new byte[bufferSize];
  44. long savedLength = 0;
  45. long imgLength = imageStream.Length;
  46. using (FileStream fs = new FileStream(Server.MapPath(url), FileMode.Create))
  47. {
  48. //write it back
  49. imageStream.Position = 0;
  50. while (savedLength < imgLength)
  51. {
  52. int bytes = imageStream.Read(buffer, 0, bufferSize);
  53. fs.Write(buffer, 0, bytes);
  54. savedLength += bytes;
  55. }
  56. }
  57. //and for the thumbnail
  58. savedLength = 0;
  59. imgLength = thumbStream.Length;
  60. //make a new URL for the thumbnail to be saved to
  61. string extention = url.Substring(url.Length - 4, 4);
  62. string thumb_url = url.Substring(0, url.Length - 4) + "_thumb" + extention;
  63. //save the thumbnail
  64. using (FileStream fs = new FileStream(Server.MapPath(thumb_url), FileMode.Create))
  65. {
  66. //write it back
  67. thumbStream.Position = 0;
  68. while (savedLength < imgLength)
  69. {
  70. int bytes = thumbStream.Read(buffer, 0, bufferSize);
  71. fs.Write(buffer, 0, bytes);
  72. savedLength += bytes;
  73. }
  74. }
  75. }
  76. protected bool ThumbnailCallback()
  77. {
  78. return true; //can't remember why this is here
  79. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.