Posted By

vedgiee on 11/17/10


Tagged

vbscript


Versions (?)

ASP.NET VB Image Upload and Resize


 / Published in: ASP
 

  1. <%@ Page Trace="False" Language="vb" aspcompat="false" debug="true" validateRequest="false"%>
  2. <%@ Import Namespace=System.Drawing %>
  3. <%@ Import Namespace=System.Drawing.Imaging %>
  4. <%@ Import Namespace=System %>
  5. <%@ Import Namespace=System.Collections %>
  6. <%@ Import Namespace=System.Web %>
  7. <%@ Import Namespace=System.Web.UI %>
  8. <%@ Import Namespace=System.Web.UI.WebControls %>
  9. <SCRIPT LANGUAGE="VBScript" runat="server">
  10. const Lx = 200 ' max width for thumbnails
  11. const Ly = 240 ' max height for thumbnails
  12. Dim upload_dir = "uploads/" ' directory to upload file
  13. Dim upload_original = ""
  14. ' filename to save original as (suffix added by script)
  15. Dim upload_thumb = "_thumb" ' filename to save thumbnail as (suffix added by script)
  16. Const upload_max_size = 250 ' max size of the upload (KB) note: this doesn't override any server upload limits
  17. Dim sFileName = ""
  18. dim fileExt ' used to store the file extension (saves finding it mulitple times)
  19. dim newWidth, newHeight as integer ' new width/height for the thumbnail
  20. dim l2 ' temp variable used when calculating new size
  21. dim fileFld as HTTPPostedFile ' used to grab the file upload from the form
  22. Dim originalimg As System.Drawing.Image ' used to hold the original image
  23. dim msg ' display results
  24. dim upload_ok as boolean ' did the upload work ?
  25. </script>
  26. <%
  27.  
  28.  
  29. randomize() ' used to help the cache-busting on the preview images
  30. upload_ok = false
  31. if lcase(Request.ServerVariables("REQUEST_METHOD"))="post" then
  32. fileFld = Request.Files(0) ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)
  33.  
  34. upload_original = fileFld.FileName
  35.  
  36.  
  37. sFileName = Left(upload_original, Len(upload_original) - 4)
  38.  
  39.  
  40. if fileFld.ContentLength > upload_max_size * 1024 then
  41. msg = "Sorry, the image must be less than " & upload_max_size & "Kb"
  42. else
  43. try
  44. originalImg = System.Drawing.Image.FromStream(fileFld.InputStream)
  45. ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
  46. ' Note: if the original is smaller than the thumbnail size it will be scaled up
  47. If (originalImg.Width/Lx) > (originalImg.Width/Ly) Then
  48. L2 = originalImg.Width
  49. newWidth = Lx
  50. newHeight = originalImg.Height * (Lx / L2)
  51. if newHeight > Ly then
  52. newWidth = newWidth * (Ly / newHeight)
  53. newHeight = Ly
  54. end if
  55. Else
  56. L2 = originalImg.Height
  57. newHeight = Ly
  58. newWidth = originalImg.Width * (Ly / L2)
  59. if newWidth > Lx then
  60. newHeight = newHeight * (Lx / newWidth)
  61. newWidth = Lx
  62. end if
  63. End If
  64.  
  65. Dim thumb As New Bitmap(newWidth, newHeight)
  66.  
  67. 'Create a graphics object
  68. Dim gr_dest As Graphics = Graphics.FromImage(thumb)
  69.  
  70. ' just in case it's a transparent GIF force the bg to white
  71. dim sb = new SolidBrush(System.Drawing.Color.White)
  72. gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)
  73.  
  74. 'Re-draw the image to the specified height and width
  75. gr_dest.DrawImage(originalImg, 0, 0, thumb.Width, thumb.Height)
  76.  
  77. try
  78. fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()
  79. originalimg.Save(Server.MapPath(upload_dir & sFileName & fileExt), originalimg.RawFormat)
  80. thumb.Save(Server.MapPath(upload_dir & sFileName & upload_thumb & fileExt), originalimg.RawFormat)
  81. msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & sFileName & fileExt)
  82. upload_ok = true
  83. catch
  84. msg = "Sorry, there was a problem saving the image."
  85. end try
  86. ' Housekeeping for the generated thumbnail
  87. if not thumb is nothing then
  88. thumb.Dispose()
  89. thumb = nothing
  90. end if
  91. catch
  92. msg = "Sorry, that was not an image we could process."
  93. end try
  94. end if
  95.  
  96. ' House Keeping !
  97. if not originalImg is nothing then
  98. originalImg.Dispose()
  99. originalImg = nothing
  100. end if
  101.  
  102. end if
  103. %>
  104. <html>
  105. <head>
  106. <title>ASP.NET File Upload and Resize Sample</title>
  107. <META NAME="Description" CONTENT="ASP.NET File Upload and Resize Sample (Hybrid VB.NET)">
  108. <META NAME="Keywords" CONTENT="ASP.NET, ASP, NET, VB, VBScript, Image, Upload, Resize, Thumbnail, Constrain, Filesize, File, Size, Free">
  109. <META NAME="Copyright" CONTENT="Rufan-Redi Pty Ltd 2005">
  110. <META NAME="Author" CONTENT="System developed by Jeremy at http://www.Rufan-Redi.com">
  111. <style type="text/css">
  112. .style1 {
  113. width: 144px;
  114. }
  115. </style>
  116. </head>
  117. <body>
  118.  
  119. <form id="Form1" enctype="multipart/form-data" method="post" runat="server">
  120. <table>
  121. <tr><td class="style1">Select the file to upload:</td><td>
  122. <input type="file" name="upload_file"
  123. id="upload_file" title="upload_file"></td></tr>
  124. <tr><td colspan=2>Max upload size <%=upload_max_size%>Kb, gif/jpg/png only</td></tr>
  125. <tr><td colspan=2><input type="submit" value="Upload"></td></tr>
  126. </table>
  127. </form>
  128.  
  129. <%
  130. if upload_ok then
  131. %>
  132. <table>
  133. <tr>
  134. <td valign=top><img src="<%=upload_dir & sFileName & fileExt & "?" & rnd()%>"></td>
  135. <td valign=top><img src="<%=upload_dir & sFileName & upload_thumb & fileExt & "?" & rnd()%>"></td>
  136. </tr>
  137. </table>
  138. <%
  139. else
  140. response.write(msg)
  141. end if
  142. %>
  143.  
  144. </body>
  145. </html>

Report this snippet  

You need to login to post a comment.