Click here to Skip to main content
15,900,495 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
i want to validate image , validate its height and width when user trying to upload, also if use upload as define image size so he can be allow to do crop as per define height and width i wrote code for image validation but its not working also i do debugging but code not going inside if condition which i highlighted


What I have tried:

OpenFileDialog openfd = new OpenFileDialog();
openfd.Filter = "Image only. |*.jpg; *.jpeg; *.png; *.gif;";
if (openfd.ShowDialog() == DialogResult.OK)
{
**if (pictureBox6.Width < 120 && pictureBox6.Height < 173)
{
MessageBox.Show("size invalid");
}**
else
{
pictureBox6.Image = new Bitmap(openfd.FileName);
}



}
Posted

1 solution

You only need an image (bitmap, in this case), and not PictureBox, which is a control rendering the image, a purely redundant control which, by some reasons, is a source of enormous confusions for many inquirers of this forum.
C#
// ...
var myBitmap = new Bitmap(openfd.FileName);
int width = myBitmap.Width;
int height = myBitmap.Height;

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900