Mathematics Mathematics - 1 month ago 14
ASP.NET (C#) Question

how can I get image size (w x h) using Stream

I have this code i am using to read uploaded file, but i need to get size of image instead but not sure what code can i use

HttpFileCollection collection = _context.Request.Files;
for (int i = 0; i < collection.Count; i++)
{
HttpPostedFile postedFile = collection[i];

Stream fileStream = postedFile.InputStream;
fileStream.Position = 0;
byte[] fileContents = new byte[postedFile.ContentLength];
fileStream.Read(fileContents, 0, postedFile.ContentLength);


I can get the file right but how to check it's image (width and size) sir ?

Rob Rob
Answer

First you have to write the image:

System.Drawing.Image image = System.Drawing.Image.FromStream (new System.IO.MemoryStream(byteArrayHere));

and afterwards you have the :

image.Height.ToString(); 

and the

image.Width.ToString();

note: you might want to add a check to be sure it's an image that was uploaded?

Comments