Set max allowed height and width on picture uploads

Hi. I have a pretty simple fileupload in wich i let users upload pictures to a webpage. I need to somehow configure the fileupload to not accept files that are bigger than a spcified width and height. Could anyone help me write some code that checks this. I assume the picture needs to be checked server side and then discarded if it doesnt meet the requierments, but i have no idea on hwo to acomplish this.

See the code bellow:

        if (FileUpload1.HasFile)
        {
            try
            {
                FileUpload1.SaveAs(@"C:\www\\files\" + FileUpload1.FileName);
                Session["picval"] = FileUpload1.FileName.ToString();
                Session["done"] = null;
            }
            catch (Exception ex)
            {
                LabelStatus.Text = "Picture was not uploaded:<br>" + ex.ToString();
                Session["done"] = "no";
            }
        }
        else
        {
            LabelStatus.Text = "You need to select something to upload!";
            Session["done"] = "no";
        }

If you wonder why i have the session["done"] variable, it's because i need to check if the file was properly uploaded before i send the article that goes with the picture to the sql datebase.
flummsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ashish PatelCommented:
you can have this below code snippet to get the dimensions of uploaded file and then use this function.

Also change the 20 in the function to your deserved size by pixels.
public bool ValidateFileDimensions(){  
	using (System.Drawing.Image myImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)) 
	{    
		return (myImage.Height == 20 && myImage.Width == 20);  
	}
} 

Open in new window

0
bmcvicarCommented:
I just found an tutorial on this:
http://www.stardeveloper.com/articles/display.html?article=2003040501&page=1

Page 3 has the code to check dimension.
http://www.stardeveloper.com/articles/display.html?article=2003040501&page=3

The tutorial also has some code to resize an image which may or may not be useful to you.
0
flummsterAuthor Commented:
Thanks i read hte guide but found it hard to incorperate with my code. I assume the ValidateFileDimensions() vill either return true or false so i tried just adding it to an if statement but i dont think Im dooing it right.

    public bool ValidateFileDimensions()
    {
        using (System.Drawing.Image myImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream))
        {
            return (myImage.Height == 400 && myImage.Width == 400);
        }
    }

    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        if ((FileUpload1.HasFile) && (CBnoPic.Checked == false) && (ValidateFileDimensions == false))
        {
            try
            {
                FileUpload1.SaveAs(@"C:\www\files\" + FileUpload1.FileName);
                Session["picval"] = FileUpload1.FileName.ToString();
                Session["done"] = null;
            }
            catch (Exception ex)
            {
                LabelStatus.Text = "Picture cpuld not be uploaded: <br>" + ex.ToString();
                Session["done"] = "no";
            }
        }
        else if ((FileUpload1.HasFile == false) && (CBnoPic.Checked == false))
        {
            LabelStatus.Text = "You have to select something to upload!";
            Session["done"] = "no";
        }
        else
        {
            LabelStatus.Text = "error!";
        }

doing ValidateFileDimensions == false threw me an exception CS1656: Cannot assign to 'ValidateFileDimensions' because it is a 'method group'. Even if i didnt get that exception i dont think it has the ability to check the ficture size untill its actually sent to the server. any ideas on how i can get the function to work with my code. WIll i need to send the file to the seerver or can it be checked client side? If it's sent how can i discard it if its to big?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Ashish PatelCommented:
Here, try this.
    public bool CheckFileDimensions()
    {
        using (System.Drawing.Image myImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream))
        {
            return (myImage.Height == 400 && myImage.Width == 400);
        }
    }
 
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        if ((FileUpload1.HasFile) && (CBnoPic.Checked == false) && (ValidateFileDimensions == false))
        {
            try
            {
                FileUpload1.SaveAs(@"C:\www\files\" + FileUpload1.FileName);
                bool anssize = CheckFileDimensions();
                //write something back to the response if anssize is false.
                if (anssize ==false) {
                     LabelStatus.Text = "Picture size is not proper : <br>";
                     Session["done"] = "no";
                } else {
                     Session["picval"] = FileUpload1.FileName.ToString();
                     Session["done"] = null;
                }
            }
            catch (Exception ex)
            {
                LabelStatus.Text = "Picture cpuld not be uploaded: <br>" + ex.ToString();
                Session["done"] = "no";
            }
        }
        else if ((FileUpload1.HasFile == false) && (CBnoPic.Checked == false))
        {
            LabelStatus.Text = "You have to select something to upload!";
            Session["done"] = "no";
        }
        else
        {
            LabelStatus.Text = "error!";
        }

Open in new window

0
flummsterAuthor Commented:
Thanks for helping me out i really aprichiate it. I tried the modified code and i see what the difference was. Howeer i seem to have run in to a small problem.

CS0103: The name 'CheckFileDimensions' does not exist in the current context

The function has the public instead of private tag so shouldnt that mean the function can be called anywhere?
0
Ashish PatelCommented:
Okay now try this, i have added the code from function in your button_click.
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        if ((FileUpload1.HasFile) && (CBnoPic.Checked == false) && (ValidateFileDimensions == false))
        {
            try
            {
                FileUpload1.SaveAs(@"C:\www\files\" + FileUpload1.FileName);
                System.Drawing.Image myImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
                //write something back to the response if anssize is false.
                if (myImage.Height != 400 && myImage.Width != 400) {
                     LabelStatus.Text = "Picture size is not proper : <br>";
                     Session["done"] = "no";
                } else {
                     Session["picval"] = FileUpload1.FileName.ToString();
                     Session["done"] = null;
                }
            }
            catch (Exception ex)
            {
                LabelStatus.Text = "Picture cpuld not be uploaded: <br>" + ex.ToString();
                Session["done"] = "no";
            }
        }
        else if ((FileUpload1.HasFile == false) && (CBnoPic.Checked == false))
        {
            LabelStatus.Text = "You have to select something to upload!";
            Session["done"] = "no";
        }
        else
        {
            LabelStatus.Text = "error!";
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
flummsterAuthor Commented:
Thanks for all your help, it seems to be working just the way i want it now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.