Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check uploaded image's dimensions

I'm using asp.net 3.5 and c# on my web site. Here is my question:

I have an upload button and asp:Image on a page. An user can upload an image from his computer and that image will be displayed in the asp:image. But before I display the image, I would like to check the width and height of the uploaded image. How do I do this?

like image 861
chuckyCheese Avatar asked Mar 04 '11 05:03

chuckyCheese


People also ask

How do I find out the size of an image?

If you right-click an image file in Windows Explorer and select Properties, you obtain the image information with the pixel size. You can see how many pixels the image is made up of, and the image resolution.

How do you check the size of an image in HTML?

First, right click on any part of the page and select “inspect element”. Now click the button at the top of the new area at the bottom of your browser window. When that icon is blue you can hover over any element on the page and information will appear such as image dimensions and container dimensions.

How do you find the width and height of a File?

Click File and then Properties. In the Properties window, under the File tab, the width and height are listed next to Video size: listing. For example, 640 x 480 is 640 width and 480 is the height.

How do I get the height and width of an uploaded image in react JS?

src = uploadedImage; console. log(image. naturalWidth,image. naturalHeight);


2 Answers

In ASP.NET you typically have the byte[] or the Stream when a file is uploaded. Below, I show you one way to do this where bytes is the byte[] of the file uploaded. If you're saving the file fisrt then you have a physical file. and you can use what @Jakob or @Fun Mun Pieng have shown you.

Either ways, be SURE to dispose your Image instance like I've shown here. That's very important (the others have not shown this).

  using (Stream memStream = new MemoryStream(bytes))
  {
    using (Image img = System.Drawing.Image.FromStream(memStream))
    {
      int width = img.Width;
      int height = img.Height;
    }
  }
like image 170
Shiv Kumar Avatar answered Oct 01 '22 06:10

Shiv Kumar


Try the following:

public bool ValidateFileDimensions()
{
    using(System.Drawing.Image myImage =
           System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream))
    {
        return (myImage.Height == 140 && myImage.Width == 140);
    }
}
like image 36
AquaticLyf Avatar answered Oct 01 '22 06:10

AquaticLyf