Revision: 36042
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at November 17, 2010 04:02 by vedgiee
Initial Code
<%@ Page Trace="False" Language="vb" aspcompat="false" debug="true" validateRequest="false"%> <%@ Import Namespace=System.Drawing %> <%@ Import Namespace=System.Drawing.Imaging %> <%@ Import Namespace=System %> <%@ Import Namespace=System.Collections %> <%@ Import Namespace=System.Web %> <%@ Import Namespace=System.Web.UI %> <%@ Import Namespace=System.Web.UI.WebControls %> <SCRIPT LANGUAGE="VBScript" runat="server"> const Lx = 200 ' max width for thumbnails const Ly = 240 ' max height for thumbnails Dim upload_dir = "uploads/" ' directory to upload file Dim upload_original = "" ' filename to save original as (suffix added by script) Dim upload_thumb = "_thumb" ' filename to save thumbnail as (suffix added by script) Const upload_max_size = 250 ' max size of the upload (KB) note: this doesn't override any server upload limits Dim sFileName = "" dim fileExt ' used to store the file extension (saves finding it mulitple times) dim newWidth, newHeight as integer ' new width/height for the thumbnail dim l2 ' temp variable used when calculating new size dim fileFld as HTTPPostedFile ' used to grab the file upload from the form Dim originalimg As System.Drawing.Image ' used to hold the original image dim msg ' display results dim upload_ok as boolean ' did the upload work ? </script> <% randomize() ' used to help the cache-busting on the preview images upload_ok = false if lcase(Request.ServerVariables("REQUEST_METHOD"))="post" then fileFld = Request.Files(0) ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image) upload_original = fileFld.FileName sFileName = Left(upload_original, Len(upload_original) - 4) if fileFld.ContentLength > upload_max_size * 1024 then msg = "Sorry, the image must be less than " & upload_max_size & "Kb" else try originalImg = System.Drawing.Image.FromStream(fileFld.InputStream) ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height ' Note: if the original is smaller than the thumbnail size it will be scaled up If (originalImg.Width/Lx) > (originalImg.Width/Ly) Then L2 = originalImg.Width newWidth = Lx newHeight = originalImg.Height * (Lx / L2) if newHeight > Ly then newWidth = newWidth * (Ly / newHeight) newHeight = Ly end if Else L2 = originalImg.Height newHeight = Ly newWidth = originalImg.Width * (Ly / L2) if newWidth > Lx then newHeight = newHeight * (Lx / newWidth) newWidth = Lx end if End If Dim thumb As New Bitmap(newWidth, newHeight) 'Create a graphics object Dim gr_dest As Graphics = Graphics.FromImage(thumb) ' just in case it's a transparent GIF force the bg to white dim sb = new SolidBrush(System.Drawing.Color.White) gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height) 'Re-draw the image to the specified height and width gr_dest.DrawImage(originalImg, 0, 0, thumb.Width, thumb.Height) try fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower() originalimg.Save(Server.MapPath(upload_dir & sFileName & fileExt), originalimg.RawFormat) thumb.Save(Server.MapPath(upload_dir & sFileName & upload_thumb & fileExt), originalimg.RawFormat) msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & sFileName & fileExt) upload_ok = true catch msg = "Sorry, there was a problem saving the image." end try ' Housekeeping for the generated thumbnail if not thumb is nothing then thumb.Dispose() thumb = nothing end if catch msg = "Sorry, that was not an image we could process." end try end if ' House Keeping ! if not originalImg is nothing then originalImg.Dispose() originalImg = nothing end if end if %> <html> <head> <title>ASP.NET File Upload and Resize Sample</title> <META NAME="Description" CONTENT="ASP.NET File Upload and Resize Sample (Hybrid VB.NET)"> <META NAME="Keywords" CONTENT="ASP.NET, ASP, NET, VB, VBScript, Image, Upload, Resize, Thumbnail, Constrain, Filesize, File, Size, Free"> <META NAME="Copyright" CONTENT="Rufan-Redi Pty Ltd 2005"> <META NAME="Author" CONTENT="System developed by Jeremy at http://www.Rufan-Redi.com"> <style type="text/css"> .style1 { width: 144px; } </style> </head> <body> <form id="Form1" enctype="multipart/form-data" method="post" runat="server"> <table> <tr><td class="style1">Select the file to upload:</td><td> <input type="file" name="upload_file" id="upload_file" title="upload_file"></td></tr> <tr><td colspan=2>Max upload size <%=upload_max_size%>Kb, gif/jpg/png only</td></tr> <tr><td colspan=2><input type="submit" value="Upload"></td></tr> </table> </form> <% if upload_ok then %> <table> <tr> <td valign=top><img src="<%=upload_dir & sFileName & fileExt & "?" & rnd()%>"></td> <td valign=top><img src="<%=upload_dir & sFileName & upload_thumb & fileExt & "?" & rnd()%>"></td> </tr> </table> <% else response.write(msg) end if %> </body> </html>
Initial URL
Initial Description
Initial Title
ASP.NET VB Image Upload and Resize
Initial Tags
Initial Language
ASP