tags:

views:

311

answers:

4

Various flavours of this question have been asked but I haven't found a correct answer yet.

Say i have an .jpg image on the file server and that I need to get its height and width. How do I do it in asp.net?

I've seen a few answers which suggests to do something like this:

System.Drawing.Image image=System.Drawing.Image.FromFile(PicturePath); 
int ActualWidth=image.Width;
int ActualHeight=image.Height;
image.Dispose();

This would work fine except that classes within the System.Drawing namespace are not supported for use within an ASP.NET service.

So, how do you get the actual height and width of an image in ASP.net?

A: 

That says Service, not Application. That will work just fine.

Daniel A. White
The ASP worker process does not run as a service?
jball
http://dotnet.org.za/eduard/archive/2004/09/23/4226.aspx
jball
A: 

add an server side image control on the aspx

<asp:image ID="img1" runat="server" src="" />

and on the code behind give it a src

img1.src = System.Drawing.Image.FromFile(PicturePath);

int ActualWidth = img1.Width;
int ActualHeight = img1.Height;
img1.src = "";
peacmaker
System.Drawing shouldn't be used in ASP.net according to the MSDN page mentioned in the question.
Anthony
img1.src doesn't work anyway. You probably meant img1.ImageUrl
Anthony
yes exactly that
peacmaker
+1  A: 

To avoid using the System.Drawing namespace:

For GIFs, the height and width are 4-byte integers found in the header of the file. (Address 0x12 for width, 0x16 for height)

For JPGs you could try writing a function like the one found here: http://www.64lines.com/jpeg-width-height It passes in the JPG as an array of data and it gets the height and width.

James Lawruk
Thanks but your link doesn’t work.
Anthony
Sorry Anothony, fixed it.
James Lawruk
A: 

The answer is in this post.

Anthony