0
Posted on 12:52 AM by prajeesh and filed under , ,
Many asp.net communities and discussion forums are flooded with questions on how to resize images while uploading or how to generate image thumbnails while uploading an image, i have seen many posts giving the solution, here i am going to combine some solutions and created a simple solution.
Another feature of this solution is you can upload any number of images at a time.
Here is the code:
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.HtmlControls;  
  8. using System.Web.UI.WebControls;  
  9. using System.Drawing;  
  10. using System.Drawing.Drawing2D;  
  11. using System.Drawing.Imaging;  
  12. using System.IO;  
  13. /// <summary>  
  14. /// Summary description for clsImageUpload : Credits : http://prajeeshkk.blogspot.com  
  15. /// </summary>  
  16. public class clsImageUpload  
  17. {  
  18. string fileName;  
  19. public clsImageUpload()  
  20. {  
  21. //  
  22. // TODO: Add constructor logic here  
  23. //  
  24. }  
  25. //This function is called from aspnet page, it takes directory name as parameter  
  26. public string HandleUploadedFile(string directory)  
  27. {  
  28. // To get the root of the web site  
  29. string root = HttpContext.Current.Server.MapPath("~/");  
  30. // clean up the path  
  31. if (!root.EndsWith(@"\"))  
  32. root += @"\";  
  33. // make a folder to store the images in  
  34. string fileDirectory = root + @"\" + directory + "\\";  
  35. // create the folder if it does not exist  
  36. // make a link to the new file  
  37.   
  38. // loop through the file in the request  
  39. for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)  
  40. {  
  41. // get the file instance  
  42. HttpPostedFile fi = HttpContext.Current.Request.Files.Get(i);  
  43. // create a byte array to store the file bytes  
  44. byte[] fileBytes = new byte[fi.ContentLength];  
  45. // fill the byte array  
  46. using (System.IO.Stream stream = fi.InputStream)  
  47. {  
  48. stream.Read(fileBytes, 0, fi.ContentLength);  
  49. }  
  50. // create a random file name  
  51. fileName = Guid.NewGuid().ToString();  
  52.   
  53. // write the resized file to the file system  
  54. File.WriteAllBytes(fileDirectory + fileName + "_thumb.jpg", ResizeImageFile(fileBytes, 75));  
  55. fileBytes = null;  
  56. }  
  57. return (fileName + "_thumb.jpg");  
  58. }  
  59. public void HandleUploadedFileUseExistingName(string directory, string fname)  
  60. {  
  61. // get the root of the web site  
  62. string root = HttpContext.Current.Server.MapPath("~/");  
  63. // clean up the path  
  64. if (!root.EndsWith(@"\"))  
  65. root += @"\";  
  66. // make a folder to store the images in  
  67. string fileDirectory = root + @"\" + directory + "\\";  
  68. // loop through the file in the request  
  69. for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)  
  70. {  
  71. // get the file instance  
  72. HttpPostedFile fi = HttpContext.Current.Request.Files.Get(i);  
  73. // create a byte array to store the file bytes  
  74. byte[] fileBytes = new byte[fi.ContentLength];  
  75. // fill the byte array  
  76. using (System.IO.Stream stream = fi.InputStream)  
  77. {  
  78. stream.Read(fileBytes, 0, fi.ContentLength);  
  79. }  
  80. // create a random file name  
  81. fileName = fname;  
  82. // write the resized file to the file system  
  83. File.WriteAllBytes(fileDirectory + fileName, ResizeImageFile(fileBytes, 75));  
  84. fileBytes = null;  
  85. }  
  86. }  
  87. /// This fuction returns a Byte array containing the resized file  
  88. private static byte[] ResizeImageFile(byte[] imageFile, int targetSize)  
  89. {  
  90. using (System.Drawing.Image oldImage =  
  91. System.Drawing.Image.FromStream(new MemoryStream(imageFile)))  
  92. {  
  93. //If you want to maintain the propotion use following code  
  94. //Size newSize = CalculateDimensions(oldImage.Size, targetSize);  
  95. //If you want to use a fixed size use following one  
  96. Size newSize = GetDimension();  
  97. using (Bitmap newImage =  
  98. new Bitmap(newSize.Width,  
  99. newSize.Height, PixelFormat.Format24bppRgb))  
  100. {  
  101. using (Graphics canvas = Graphics.FromImage(newImage))  
  102. {  
  103. canvas.SmoothingMode = SmoothingMode.AntiAlias;  
  104. canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  105. canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;  
  106. canvas.DrawImage(oldImage,  
  107. new Rectangle(new Point(0, 0), newSize));  
  108. MemoryStream m = new MemoryStream();  
  109. newImage.Save(m, ImageFormat.Jpeg);  
  110. return m.GetBuffer();  
  111. }  
  112. }  
  113. }  
  114. }  
  115. /// This function Calculates the new size of the image based on the target size  
  116. private static Size CalculateDimensions(Size oldSize, int targetSize)  
  117. {  
  118. Size newSize = new Size();  
  119. if (oldSize.Height > oldSize.Width)  
  120. {  
  121. newSize.Width =  
  122. (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));  
  123. newSize.Height = targetSize;  
  124. }  
  125. else  
  126. {  
  127. newSize.Width = targetSize;  
  128. newSize.Height =  
  129. (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));  
  130. }  
  131. return newSize;  
  132. }  
  133. //Dimension of the images can be set here  
  134. private static Size GetDimension()  
  135. {  
  136. Size newSize = new Size();  
  137. newSize.Width = 100;  
  138. newSize.Height = 100;  
  139. return newSize;  
  140. }  
  141. }  

You can download source code from here Shout it kick it on DotNetKicks.com
0
Responses to ... Image Resizing while uploading using asp.net