我正在尝试在c#中创建一个项目,我希望将图像上传到数据库,如果它的大小<150 kb。如何设置上传图片的限制?我不知道如何扩展它?请提前帮助谢谢
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;
}
答案 0 :(得分:4)
使用FileInfo类获取文件大小。 FileInfo.Length
if (new FileInfo(ofd.FileName).Length > (150 * 1024))
{
throw new ApplicationException(); //handle invalid file size here
}