.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET: Restrict Dimensions While Uploading Images With The FileUpload Control

I just had to deal with uploading images to my server and had to figure out a way to restrict my users’ uploads to certain dimensions. It’s pretty easy to do, just not entirely intuitive at first. So, I’ll take the guesswork out of it for you ๐Ÿ™‚

My project requires that the images be no wider than 190px and must be exactly 190px tall.

First, add a FileUpload control to your form and an upload button:
<asp:FileUpload id="FileUpload1" runat="server" /><asp:Button id="Button1" runat="server" text="Upload" />

Next, I created a generic helper function for all file uploads, but you don’t have to do that. All you need to do is do the following:

Const PROD_IMG_MAX_WIDTH As Integer = 190
Const PROD_IMG_HEIGHT As Integer = 190
Const ABS_PATH as String = "[Your Server's Absolute Path, usually C:\something\weborsomethingelse\yoursite.com]"

Dim savePath As String = ABS_PATH & "\images\"

'Create an image object from the uploaded file
Dim UploadedImage As System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)

If UploadedImage.Width > PROD_IMG_MAX_WIDTH Or UploadedImage.Height <> PROD_IMG_HEIGHT Then
     'whatever fail code you need
     savePath &= FileUpload1.FileName
End If

And that’s pretty much all there is to it! Of course you can modify it to accept different folders, change the filename, or anything of that sort. The key is really determining the dimensions from the InputStream. Everything else is gravy.

NOTES: The FileUpload control is only available in .NET 2.0. Also, if your FileUpload control is on an AJAX-ified page, you will need to do some trigger fixes, since you can’t upload a file using AJAX, at least not yet (easily). Lastly, you need to make sure that your server will allow you to write to it from the web. If you’re having problems, talk to your host to have them grant you upload/write access to the server.

kick it on DotNetKicks.com


November 27, 2007 - Posted by | ASP.NET, Tips & Tricks |


  1. […] .NET Discussion put an intriguing blog post on ASP.NET: Restrict Dimensions While Uploading Images With The FileUpload Control […]

    Pingback by ASP.NET: Restrict Dimensions While Uploading Images With The FileUpload Control | November 27, 2007 | Reply

  2. The problem here is that the image actually has to be uploaded before you can determine if it is too large. This might not be that much of an issue, but you could programmatically resize the image on the fly if it is too big, which would be very cool…


    Best regards…

    Comment by Josh Stodola | November 27, 2007 | Reply

  3. Damn wordpress is messin with my links again. Now you know why I wrote my own blog ๐Ÿ˜€

    Comment by Josh Stodola | November 27, 2007 | Reply

  4. true, you do have to “upload” the image, but you don’t have to save it on your server, which is done at the FileUpload1.SaveAs() function, so if they want to try to upload a ginormous file and wait to find out if it’s ok, that’s their prerogative; it’s not making it to the server ๐Ÿ™‚

    Plus, what I didn’t put on here is a way to make sure that the file is actually an image by using a regex validator. Hmm, maybe I should do that.

    Comment by Some.Net(Guy) | November 27, 2007 | Reply

  5. “I didnโ€™t put on here is a way to make sure that the file is actually an image”

    do you specify a file type filter in ASP.NET just like you do with the openFileDialog in windows forms?

    I just did something like that last Friday.. but didn’t analyze if it would confirm that only an .jpg, etc was selected. I better look at that tomorrow ๐Ÿ™‚

    Are you saving the images in a database record or just saving the path\imagename in the database and storing the files elsewhere?

    I choose to store the files elsewhere. we’ll be accessing them with mobiles and didn’t want to slow up everything having one mobile pull in image files that it may not even need.

    … still, images *in* a database record sounds cool and I’ve wanted to have a reason to do it. ๐Ÿ™‚

    Comment by hobbylobby | December 2, 2007 | Reply

  6. the way i would specify images is by including a regex validator that would make sure the extension on the file being uploaded is either .jpg or .gif. the regex expression i use is


    this only makes sure it’s a .jpg, but you could put .gif|.GIF in there too if you wanted. as for storing images *IN* a database, that’s never recommended, since they take up so much room and are slow to retrieve from a database. your best bet (and the web best-practices method) is to store them in an /images/ folder and store their path (relative or absolute, but preferably relative) in your database.

    Comment by Some.Net(Guy) | December 3, 2007 | Reply

  7. >storing images *IN* a database, thatโ€™s never recommended

    I’m still cool then ๐Ÿ™‚

    I love regexes. don’t try to write them but love em ๐Ÿ™‚
    you write that?

    Comment by hobbylobby | December 3, 2007 | Reply

  8. haha, yeah you’re still good. regexes are wonderful for certain things, but it’s sooo complicated. i wrote a little bit of that regex, but mostly i got it from visual studios. modified by me tho! ๐Ÿ™‚

    Comment by Some.Net(Guy) | December 4, 2007 | Reply

  9. This is exactly what I’m looking for to determine the image’s dimension. Thanks!

    Comment by wikigiz | April 14, 2008 | Reply

  10. Have you noticed that the Regex doesn’t work in Mozilla, if the folder and filename has spaces in it? Wierd…Not sure how to fix this…

    Comment by Wade Walker | June 24, 2008 | Reply

  11. Don’t use folders and filenames with spaces ๐Ÿ™‚ I don’t know either. There’s probably a better regex you could write, but by web best practices, you shouldn’t use spaces in your folders/filenames anyways.

    Comment by Some.Net(Guy) | June 24, 2008 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: