Check the width and height of an image
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;
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");
}