Posted By

vedgiee on 11/11/10


Tagged

aspnet


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

franksmile0310
tonicglop


ASP.NET Image Upload and Resize


 / Published in: C#
 

Image Upload and Resize

  1. public void ResizeFromStream(string ImageSavePath, int MaxSideSize, Stream Buffer)
  2. {
  3. int intNewWidth;
  4. int intNewHeight;
  5. Image imgInput = Image.FromStream(Buffer);
  6.  
  7. //Determine image format
  8. ImageFormat fmtImageFormat = imgInput.RawFormat;
  9.  
  10. //get image original width and height
  11. int intOldWidth = imgInput.Width;
  12. int intOldHeight = imgInput.Height;
  13.  
  14. //determine if landscape or portrait
  15. int intMaxSide;
  16.  
  17. if (intOldWidth >= intOldHeight)
  18. {
  19. intMaxSide = intOldWidth;
  20. }
  21. else
  22. {
  23. intMaxSide = intOldHeight;
  24. }
  25.  
  26.  
  27. if (intMaxSide > MaxSideSize)
  28. {
  29. //set new width and height
  30. double dblCoef = MaxSideSize / (double)intMaxSide;
  31. i ntNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
  32. intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
  33. }
  34. else
  35. {
  36. intNewWidth = intOldWidth;
  37. intNewHeight = intOldHeight;
  38. }
  39. //create new bitmap
  40. Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);
  41.  
  42. //save bitmap to disk
  43. bmpResized.Save(ImageSavePath, fmtImageFormat);
  44.  
  45. //release used resources
  46. imgInput.Dispose();
  47. bmpResized.Dispose();
  48. Buffer.Close();
  49. }

Report this snippet  

You need to login to post a comment.