Getting the Actual Dimensions of an Uploaded Image

Suppose you allow images to be uploaded on your site but they must be of a certain size or maybe you want to resize the image as a percent of the original size. The hurdle by doing it in javascript or jquery is that you have to wait until it is completely loaded or you will get zeros as its dimension. Secondly using Javascript to get the image will give you the image size of the container it is in. So if you create a place holder then you cannot use width or size on the image or the result will be that image container. In other words something like this…

<img src="www.mywebsite.com/img/1.jpg" width="32px" height="32px"/>

Will return 32px even though the image might be 900px x 900px. This approach will only work if you do not put the size inside the image.

One solution to this is to run the code on the server. This will provide the actual dimensions. From there you can actually continue processing it or return a message that it needs to be the right size.

//Put the uploaded image into an image object
Dim UploadedImage as System.Drawing.Image = System.Drawing.Image.FromStream(UploadedFile.PostedFile.InputStream)
//Capture the width and height of uploaded image
 Dim UploadedImageWidth as float = UploadedImage.PhysicalDimension.Width
Dim UploadedImageHeight as float = UploadedImage.PhysicalDimension.Height
If......
end if

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , | Posted under ASP .NET 4.0, Programming/Coding | RSS 2.0

Author Spotlight

David Bauernschmidt

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

Leave a Reply

Your email address will not be published. Required fields are marked *


− 5 = 3

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>