3

On upload of an image file, I want to check its width/height. So far the only way I have found to this is to create an image file out of it either though accessing the memory stream or saving the file o disk, both of which are memory intensive operations.

Perhaps its possible to read the metadata of the file, though I suspect that differences between image types will make this process cumbersome.

CodeToad
  • 4,656
  • 6
  • 41
  • 53

2 Answers2

8
// File - have type HttpPostedFileBase
var img = Drawing.Image.FromStream(File.InputStream, true, true);
int w = img.Width;
int h = img.Height;
Антон К
  • 81
  • 1
  • 3
  • 3
    Hi Welcome to stack overflow! generally we prefer explanation of code to go along with solutions to help with learning. You might take a look at [How to Write A Good Answer](http://stackoverflow.com/help/how-to-answer) – user314321 Jan 14 '16 at 15:33
1

Pretty sure there's not a way to do it on the server side without first converting it to an image because the HttpPostedFileBase type could be really any type of file...not just an image.

This may be solution on the client side though.

Community
  • 1
  • 1
RayLoveless
  • 19,880
  • 21
  • 76
  • 94
  • Good idea. I am already showing a preview of the image on the client side using data url, so I have the dimensions available at that time. – CodeToad Jan 17 '16 at 07:46