Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Image Upload and Resizing Question Answered


Message #1 by "Scott Guthrie" <scottgu@m...> on Fri, 23 Mar 2001 02:58:56 -0800
Origional Question:

-----------------------------------------



I need to resize image which was just uploaded to the server. Image can

be

any format and I need to save it as JPG. I have been using ASPImage

before.

Is it possible to do this using .NET.

Please Help





ScottGu Answer:

-----------------------------------



Hi Tom,



Its actually really easy to do this with .NET -- both the file upload

part as well as the image resizing and save as JPG part.  The below

sample I've put together demonstrates how to do this (just copy/save it

into a file called "ImageResizer.aspx" and hit it with a browser.  It

allows you to select an image on the client, upload it to the server,

load it into an image object (without having to save it to disk), resize

it, and then persist it to disk as a JPG.



The file upload part of the solution is accomplished simply using the

new file upload support within ASP.NET.  The Image manipulation is done

using the System.Drawing namespace -- which provides all of our graphic

manipulation APIs.



Hope this helps,



Scott



------------------------------------------------------------------------

---------------



<%@ Import Namespace=3D"System.Drawing" %>

<%@ Import Namespace=3D"System.Drawing.Imaging" %>



<html>



   <script language=3D"VB" runat=3Dserver>

  

       Sub UploadBtn_Click(Sender as Object, E as EventArgs)

      

           ' Obtain Image Object Reference from Uploaded Filestream

           Dim uploadImage as System.Drawing.Image

           uploadImage =3D

System.Drawing.Image.FromStream(ImageUpload.PostedFile.InputStream)

          

           ' Calculate new Image Dimensions

           Dim width as Integer

           Dim height as Integer

          

           width =3D Integer.Parse(WidthTxt.Value)

           height =3D Integer.Parse(HeightTxt.Value)



           ' Obtain Resized Image Object From Uploaded Image

           Dim resizedImage as System.Drawing.Image

           resizedImage =3D uploadImage.GetThumbnailImage(width, height,

Nothing, 0)



           ' Save Image to disk as a JPG

           resizedImage.Save(Server.MapPath("test.jpg"),

ImageFormat.JPEG)

          

           ' Redirect browser to Image

           Response.Redirect("test.jpg")



       End Sub



   </script>



   <body>

  

      <form enctype=3D"multipart/form-data" runat=3Dserver>

     

           <h1>Welcome to ScottGu's Image Resizer</h1>

          

           Image to Upload: <input id=3D"ImageUpload" type=3D"file"

runat=3Dserver>

          

           <br><br>

          

           New Horizontal Size: <input id=3D"WidthTxt" type=3D"text"

value=3D"200" runat=3Dserver> <br>

           New Veritical Size: <input id=3D"HeightTxt" type=3D"text"

value=3D"400" runat=3Dserver>

          

           <input text=3D"Upload and Resize" type=3D"submit"

OnServerClick=3D"UploadBtn_Click" runat=3Dserver>



      </form>

  

   </body>

</html>


  Return to Index