Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Uploading, Determining Size, Width and Height and Resizing Image Files with ASP.NET
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Last method allows files of any size to be uploaded by calling our UploadImage method.

protected void UploadImageWithNoRestrictions(HttpPostedFile file)
{
  if(IsImage(file))
  {
    UploadImage(file);
  }
}

The last method is the method which actually uploads the image and creates a new resized image ( thumbnail ) to be shown on the default.aspx page.

It first fetches the file name ( e.g. myimage.jpg ) from the complete path and file name ( e.g. C:\SomeFolder\AnotherFolder\myimage.jpg ) of the uploaded image. It then saves that image in the 'uploaded_images' folder on the server.

Next it creates a new Bitmap image from the existing image ( uploaded image ) with new width and height ( 150x150 px ). It then saves the new thumbnail image as well in the 'uploaded_images' folder by prepending the image name with 't__' e.g. t__myimage.jpg.

So after this method successfully returns, you have two images created in the 'uploaded_images' folder. So for example if the uploaded image was myimage.jpg, then the two images that get created will be myimage.jpg ( same uploaded image file ) and t__myimage.jpg ( the resized thumbnail ).

Tip: Memorize and understand the code we have used to create a new resized image file. As you will be using this code later on ( after all this is why you've read this article ).
protected void UploadImage(HttpPostedFile file)
{
  string fileName = file.FileName.Substring(file.FileName.LastIndexOf('\\') + 1);
  string filePath = MapPath("uploaded_images/" + fileName);

  file.SaveAs(filePath);
  AppendMessage("<span style=\"color:green;\">File Uploaded...</span><br>");

  using(System.Drawing.Image image =
    System.Drawing.Image.FromStream(file.InputStream))
  using(Bitmap bitmap = new Bitmap(image, ThumbnailWidth, ThumbnailHeight))
  {
    bitmap.Save(MapPath("uploaded_images/t__" + fileName), image.RawFormat);
    AppendMessage("<span style=\"color:green;\">Thumbnail " +
      " created...</span><br><br>");
  }
}

This is how upload.aspx looks like on my system before we upload any file:

Screenshot: upload.aspx
Screenshot: upload.aspx

And after we have uploaded a file:

Screenshot: upload.aspx
Screenshot: upload.aspx

Previous ( 6 Gone )( 3 Remaining ) Next

Comments/Questions ( Threads: 32, Comments: 45 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Ported to VB.NET 2.0
  2. Uploading files to server hard drive (plain ASP)
  3. Ported to VB.net
  4. compression of image size while uploading?????
  5. ASP
  6. Transparant gif images get ugly after resizing
  7. Uploading vs Saving
  8. Automatic thumbnail generation
  9. change the thumbnail size ( 1 Reply )
  10. Parser Error starting default.aspx
  11. Cannot get script to show uploaded files.. in fact they wont upload?
  12. paging pictures
  13. Convert entire application to VB.Net
  14. Sort gallery images by date instead of name ?
  15. Checking if file already exists ? ( 1 Reply )
  16. Image is used by another process
  17. ASP.Net
  18. Please help.
  19. I just cant register the uploadimghd.dll ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  20. I just cant register the uploadimghd.dll
  21. VB Script ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  22. Compression of image while uploading
  23. Error... Please help. I'd really like to get this script working! ( 1 Reply )
  24. keeping aspect ratio of the picture ( 3 Replies )
  25. want to upload file on remote server
  26. PNG and BMP Image Types Not Displaying
  27. error on execute build.bat
  28. database pull on picture view
  29. connect to internet ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  30. Convert a phrase to VB ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  31. Mini-tutorial ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  32. test

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.