Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  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)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Uploading, Determining Size, Width and Height and Resizing Image Files with ASP.NET
 

Uploading, Determining Size, Width and Height and Resizing Image Files with ASP.NET

by Faisal Khan.Follow Faisal Khan on Twitter

Overview
In this article we will learn how to determine the size and dimensions ( width & height ) of an uploaded image? allow the image to be uploaded if it is less than given size? or if it's width and height are less than given values? and finally how to resize an image to create thumbnails?

Note: The details of file uploading with ASP.NET will not be covered here. They have been covered in detail in separate articles ( see below ). Please consult those articles first if you feel uncomfortable with the code shown in this article.

File Uploading Using ASP.NET
Here are few Stardeveloper articles on this topic:

  1. File uploading to server hard disk.
  2. File uploading to Microsoft Access database.
  3. Determining size, width & height and resizing image files ( this article ).

Problem
Ok, let's get started with this article. Let's write down the requirements first:

  • Be able to upload only image files.
  • Be able to determine the size, width & height of the uploaded image file.
  • Be able to resize and create thumbnails of all uploaded image files.
  • Be able to view all the thumbnails of all uploaded images on a single page.

We'll also learn how to allow image files of certain size ( e.g. less than 3 KB ) and dimensions ( e.g. width less than 300px, height less than 300 px ) to be uploaded only if they meet our requirements. This is important because a lot of users had wanted me to explain how to allow only image files of certain size, width and height to be uploaded; I'll explain that in this article.

Design
Our application will consist of following:

  1. 3 ASP.NET pages:
    • default.aspx - displays all the thumbnails.
    • upload.aspx - allows image uploading.
    • file.aspx - view, download or delete image files.
  2. uploaded_images - for all uploaded image files ( including thumbnails ).
  3. build.bat - for compiling all code-behind classes.

The design is simple and the code is even simpler. In this article I'll only explain the code that is responsible for determining the size, width and height of uploaded image files. Rest of the code which is responsible for handling file uploads has been explained in great detail in separate articles ( see above ).

Solution
Let's start coding our ASP.NET pages.

i. default.aspx
This page will be responsible for displaying all the thumbnails of all uploaded images files in the 'uploaded_images' folder. These thumbnails are generated by upload.aspx page every time an image gets uploaded. This page will also display links to view, download and delete those image files.

Create a new default.aspx ASP.NET page and copy/paste following code in it:

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false"
	Inherits="Stardeveloper.UploadImage.DefaultForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 

<html>
<head>
	<style>
	body { margin: 0px 0px 0px 0px; padding: 10px 10px 10px 10px; }
	body, input, td, select { font: 11pt Verdana; }
	a { color: #5A7193; }
	.stdInput { width: 500px; }
	.smInput { width: 250px; }
	.dimColor { color: Gray; }
	.imgInfo { font: 8pt Verdana; }
	</style>
</head>
<body>

<table align="center" width="100%" border="1" bordercolor="silver"
	cellpadding="2" cellspacing="2">
	<tr>
		<td align="right">
		Main Page · 
		<a href="upload.aspx">Upload Images</a> 
		  
		</td>
	</tr>
</table><br>

<table align="center" width="100%" border="1" bordercolor="silver"
	cellpadding="2" cellspacing="2">
	<tr>
		<td bgcolor="#CCDDEE" align="center">
		Uploaded Images To Server Hard Disk
		</td>
	</tr>
	<tr>
		<td bgcolor="#F7F7F7" align="center">
		i. Image Files
		</td>
	</tr>
	<tr>
		<td>
		<div id="imageFiles" runat="server" />
		</td>
	</tr>
</table>
	
</body>
</html>

Overview
In this article we will learn how to determine the size and dimensions ( width & height ) of an uploaded image? allow the image to be uploaded if it is less than given size? or if it's width and height are less than given values? and finally how to resize an image to create thumbnails?

Note: The details of file uploading with ASP.NET will not be covered here. They have been covered in detail in separate articles ( see below ). Please consult those articles first if you feel uncomfortable with the code shown in this article.

File Uploading Using ASP.NET
Here are few Stardeveloper articles on this topic:

  1. File uploading to server hard disk.
  2. File uploading to Microsoft Access database.
  3. Determining size, width & height and resizing image files ( this article ).

Problem
Ok, let's get started with this article. Let's write down the requirements first:

  • Be able to upload only image files.
  • Be able to determine the size, width & height of the uploaded image file.
  • Be able to resize and create thumbnails of all uploaded image files.
  • Be able to view all the thumbnails of all uploaded images on a single page.

We'll also learn how to allow image files of certain size ( e.g. less than 3 KB ) and dimensions ( e.g. width less than 300px, height less than 300 px ) to be uploaded only if they meet our requirements. This is important because a lot of users had wanted me to explain how to allow only image files of certain size, width and height to be uploaded; I'll explain that in this article.

Design
Our application will consist of following:

  1. 3 ASP.NET pages:
    • default.aspx - displays all the thumbnails.
    • upload.aspx - allows image uploading.
    • file.aspx - view, download or delete image files.
  2. uploaded_images - for all uploaded image files ( including thumbnails ).
  3. build.bat - for compiling all code-behind classes.

The design is simple and the code is even simpler. In this article I'll only explain the code that is responsible for determining the size, width and height of uploaded image files. Rest of the code which is responsible for handling file uploads has been explained in great detail in separate articles ( see above ).

Solution
Let's start coding our ASP.NET pages.

i. default.aspx
This page will be responsible for displaying all the thumbnails of all uploaded images files in the 'uploaded_images' folder. These thumbnails are generated by upload.aspx page every time an image gets uploaded. This page will also display links to view, download and delete those image files.

Create a new default.aspx ASP.NET page and copy/paste following code in it:

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false"
	Inherits="Stardeveloper.UploadImage.DefaultForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 

<html>
<head>
	<style>
	body { margin: 0px 0px 0px 0px; padding: 10px 10px 10px 10px; }
	body, input, td, select { font: 11pt Verdana; }
	a { color: #5A7193; }
	.stdInput { width: 500px; }
	.smInput { width: 250px; }
	.dimColor { color: Gray; }
	.imgInfo { font: 8pt Verdana; }
	</style>
</head>
<body>

<table align="center" width="100%" border="1" bordercolor="silver"
	cellpadding="2" cellspacing="2">
	<tr>
		<td align="right">
		Main Page · 
		<a href="upload.aspx">Upload Images</a> 
		  
		</td>
	</tr>
</table><br>

<table align="center" width="100%" border="1" bordercolor="silver"
	cellpadding="2" cellspacing="2">
	<tr>
		<td bgcolor="#CCDDEE" align="center">
		Uploaded Images To Server Hard Disk
		</td>
	</tr>
	<tr>
		<td bgcolor="#F7F7F7" align="center">
		i. Image Files
		</td>
	</tr>
	<tr>
		<td>
		<div id="imageFiles" runat="server" />
		</td>
	</tr>
</table>
	
</body>
</html>

 ( 9 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


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 to.

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