Friday, May 17, 2013

Check image height & width in pixel during uploading image using File Upload Control.

You can check the size, height and width of image and do some restriction during file upload. Include System.Drawing namespace for this.Here ImageUploader is the File Upload Control.

//********* Chcek Image Size ************
Bitmap img = new Bitmap(ImageUploader.PostedFile.InputStream, false);
int height = img.Height;
// get the height of image in pixel.
int width = img.Width;
// get the width of image in pixel.
int fileSize = (ImageUploader.PostedFile.ContentLength) / 1024;
//get the size of image file.
if (height >= 500 && width >= 500 && fileSize > 500)
{
lblErrorMessage.BackColor = Color.Red;
lblErrorMessage.ForeColor = Color.Black;
lblErrorMessage.Text = "File size not be exceed than 500 KB,500x500 px";
return;
}

//*************** End of Block *****************

0 comments:

Post a Comment