How do I get he dimensions of  an image on upload

Posted on 2012-09-04
Last Modified: 2012-09-04
I’m trying to upload images but I would like to know the dimensions of the image so I know if it should be auto cropped.  Below is a what I have now, but how do I get the dimension of the image?

string uploadFolder = Request.PhysicalApplicationPath + "/images/";  

if (FileUpload1.HasFile)
      string oldFileName = FileUpload1.FileName.ToString();

      FileInfo info = new FileInfo(FileUpload1.PostedFile.ContentLength.ToString());

string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);

            if (extension.ToLower() == ".jpg" || extension.ToLower() == ".gif" || extension.ToLower() == ".png")
                string fileSize = FileUpload1.Width.Value.ToString();
                Label2.Text = info.ToString();
                Label1.Text = "This file extension not permitted: " + extension;


Open in new window

Question by:bmanmike39
    LVL 18

    Accepted Solution

    Try this,
    System.Drawing.Bitmap img = new System.Drawing.Bitmap(FileUpload1.PostedFile.InputStream);
    int h = img.Height;
    int w = img.Width;

    Open in new window


    Author Closing Comment

    Thanks!   Just what i needed!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now