Check the width and height of an image
Asked Answered
C

4

17

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.

Camilacamile answered 18/5, 2011 at 12:14 Comment(0)
S
45

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;
Stormy answered 18/5, 2011 at 12:17 Comment(0)
I
3
        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)
            {
                System.IO.FileInfo file = new System.IO.FileInfo(open.FileName);
                Bitmap img = new Bitmap(open.FileName);


                if (img.Width < MAX_WIDTH &&
                    img.Height < MAX_HEIGHT &&
                    file.Length < MAX_SIZE)
                    pictureBox2.Image = img;

            }
        }
        catch (Exception)
        {
            throw new ApplicationException("Failed loading image");
        }
Inbound answered 18/5, 2011 at 12:22 Comment(0)
O
1

I had a similar issue and I wrote a method to detect if the picture is landscape or not. If it can help you.

public static bool IsPictureLandscape(string fileName)
{
  try
  {
    Bitmap image = new Bitmap(fileName);
    return image.Width > image.Height;
  }
  catch (Exception)
  {
    return false;
  }
}
Ohmage answered 30/4, 2021 at 23:17 Comment(0)
S
0

UWP has currently a nice interface to obtain image properties.

        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");

        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
            // Application now has read/write access to the picked file
            ImageProperties IP = await file.Properties.GetImagePropertiesAsync();

            double Width = IP.Width;
            double Height = IP.Height;
        }
Scrutator answered 26/8, 2019 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.