By chuckyCheese

2011-03-04 05:32:02 8 Comments

I'm using 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?


@Ad Kahn 2017-07-07 12:35:11

Try this.

              public boolean CheckImgDimensions(string imgPath, int ValidWidth , int ValidHeight){  

                 var img = Image.FromFile(Server.MapPath(imgPath));

                 return (img.width == ValidWidth &&  img.height == ValidHeight );


if ( CheckImgDimensions("~/Content/img/MyPic.jpg",128,128) ){ 
     /// what u want

@Khaled Tarboosh 2013-09-09 08:08:02

Try this:

Stream ipStream = fuAttachment.PostedFile.InputStream;
using (var image = System.Drawing.Image.FromStream(ipStream))
    float w = image.PhysicalDimension.Width;
    float h = image.PhysicalDimension.Height;

@AquaticLyf 2012-09-16 14:08:27

Try the following:

public bool ValidateFileDimensions()
    using(System.Drawing.Image myImage =
        return (myImage.Height == 140 && myImage.Width == 140);

@BerggreenDK 2013-03-16 14:30:00

Yup, great for images, but has a problem with PDF and EXE if the user uploads a "none image" by fault or on purpose.

@Shiv Kumar 2011-03-04 05:49:40

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;

@BerggreenDK 2013-03-16 14:28:51

this one also crashes if you try to upload a PDF or EXE file.

@Fun Mun Pieng 2011-03-04 05:41:39

    Image img = System.Drawing.Image.FromFile("test.jpg");
    int width = img.Width;
    int height = img.Height;

You may need to add the System.Drawing reference.

You may also use the FromStream function if you have not saved the image to disk yet, but looking at how you're using the image (viewable by user in an Image control), I suspect it's already on disk. Stream to image may or may not be faster than disk to image. You might want to do some profiling to see which has better performance.

@o_o 2013-02-25 08:57:57

This works until someone uploads a malicious large image - for example 100k x 100k pixel dimensions. your server will run out of memory.

@vortexwolf 2013-11-13 15:10:28

@o_o In case of large images check the HttpPostedFileBase.ContentLength property so that no one uploads an image larger than 1-2 megabytes.

@Jakob Gade 2011-03-04 05:40:42

Load the image into an Image and check the dimensions serverside?

Image uploadedImage = Image.FromFile("uploadedimage.jpg");
// uploadedImage.Width and uploadedImage.Height will have the dimensions...

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] Is there a way to check if a file is in use?

44 Answered Questions

40 Answered Questions

[SOLVED] How to get screen dimensions as pixels in Android

17 Answered Questions

[SOLVED] Android: combining text & image on a Button or ImageButton

17 Answered Questions

[SOLVED] Change the selected value of a drop-down list with jQuery

27 Answered Questions

[SOLVED] Why not inherit from List<T>?

14 Answered Questions

[SOLVED] Type Checking: typeof, GetType, or is?

35 Answered Questions

[SOLVED] Login failed for user 'IIS APPPOOL\ASP.NET v4.0'

41 Answered Questions

[SOLVED] How do I format a Microsoft JSON date?

8 Answered Questions

Sponsored Content