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?
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.
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.
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.
src = uploadedImage; console. log(image. naturalWidth,image. naturalHeight);
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;
}
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With