检查图像的宽度和高度

bha*_*thi 13 c# height image width

我可以在图片框中显示图片,而无需通过以下代码检查文件大小:

private void button3_Click_1(object sender, EventArgs e)
{
    try
    {
        //Getting The Image From The System
        OpenFileDialog open = new OpenFileDialog();
        open.Filter =
          "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

        if (open.ShowDialog() == DialogResult.OK)
        {
            Bitmap img = new Bitmap(open.FileName);

            pictureBox2.Image = img;
        }
    }
    catch (Exception)
    {
        throw new ApplicationException("Failed loading image");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在图片框中显示之前检查图像大小,例如是2MB还是4MB.我还想检查图像的宽度高度.

Ode*_*ded 37

Bitmap将保存图像的高度和宽度.

使用该属性获取文件大小.FileInfo Length

FileInfo file = new FileInfo(open.FileName);
var sizeInBytes = file.Length;

Bitmap img = new Bitmap(open.FileName);

var imageHeight = img.Height;
var imageWidth = img.Width;

pictureBox2.Image = img;
Run Code Online (Sandbox Code Playgroud)