Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set image size limit in c# (eg; <150kb )

Tags:

I am trying to create a project in c# , I want to upload images in to database if its size is <150 kb . How to set the limitation for uploading images? I don't know how to expand it ? please help thanks in advance

private void Browsebutton3_Click(object sender, EventArgs e)
{
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "images only.|*.jpg; *.jpeg; *.png";
        DialogResult dr = ofd.ShowDialog();
        pictureBox1.Image = Image.FromFile(ofd.FileName);
        //pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

        textBox5.Text = ofd.FileName;
}
like image 214
shafeeq Avatar asked Jul 08 '16 06:07

shafeeq


1 Answers

Use the FileInfo class to get the file size. The number of bytes are accessible by FileInfo.Length

if (new FileInfo(ofd.FileName).Length > (150 * 1024))
{
    throw new ApplicationException(); //handle invalid file size here
}
like image 65
fubo Avatar answered Sep 28 '22 02:09

fubo