Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the width and height of an image

I am able to display the picture in the picture box without checking the file size by the following code:

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");
    }
}

I want to check the image size for example whether it is 2MB or 4MB before displaying in the picture box. i also want to check the width and height of the image.

like image 613
bharathi Avatar asked May 18 '11 12:05

bharathi


1 Answers

The Bitmap will hold the height and width of the image.

Use the FileInfo Length property to get the file size.

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;
like image 134
Oded Avatar answered Sep 17 '22 12:09

Oded