get picture dimensions of uploaded picture file

  • Thread starter Thread starter Kruno
  • Start date Start date
K

Kruno

HI DEAR DEVELOPERS ! :))
If anyone has 1 minute to explain me something :How to find out the width and height of a picture
that has been uploaded through HTML inputfile control
to the Server ?

I would really appreciate if you would
write me some code, not only explain me
 
Hi, Kruno,

The following static method checks if the stream passed contains a valid
image and returns through the out parameters the width and height of the
image:


public static bool GetSize(System.IO.Stream stream, out int width, out int
height)
{
width = height = 0;
try
{
using(System.Drawing.Image img =
System.Drawing.Image.FromStream(stream))
{
width = img.Width;
height = img.Height;
}
}
catch
{
return false;
}
return true;
}


Usage:

int width, height;
bool imageIsValid =
GetSize(inputFile.PostedFile.InputStream, out width, out height);

where inputFile is the reference to the
System.Web.UI.HtmlControls.HtmlInputFile control in the page.

Hope this helps
Martin
 
Back
Top