How do I do that in C#? :) Thx!
views:
530answers:
3
+2
A:
Define "valid"
write a valdation function
call it on the image
if it passes, load the image, otherwise don't
Steven A. Lowe
2010-01-29 17:25:39
+2
A:
It is pretty simple. If you can load the image before you assign it to the picture box then you've sufficiently proven that the image is valid and that the user has something to look at. The GDI+ image decoders very carefully check the file contents. Thus:
private void button1_Click(object sender, EventArgs e) {
if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return;
try {
Bitmap bmp = new Bitmap(openFileDialog1.FileName);
if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
pictureBox1.Image = bmp;
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Could not load image");
}
}
Hans Passant
2010-01-29 17:45:49