• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

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.
0
flummster
Asked:
flummster
  • 3
  • 3
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
flummsterAuthor Commented:
Thanks for all your help, it seems to be working just the way i want it now.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now