简体   繁体   English

如何在c#中设置图像大小限制(例如; <150kb)

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

I am trying to create a project in c# , I want to upload images in to database if its size is <150 kb . 我想在c#中创建一个项目,如果它的大小<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;
}

Use the FileInfo class to get the file size. 使用FileInfo类获取文件大小。 The number of bytes are accessible by FileInfo.Length FileInfo.Length可以访问字节数

if (new FileInfo(ofd.FileName).Length > (150 * 1024))
{
    throw new ApplicationException(); //handle invalid file size here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM