try { var srcImage = Image.FromFile("test.jpg"); myPictureBox.Image = srcImage; } catch (System.OutOfMemoryException) { MessageBox.Show("Invalid picture format."); }However, you will find that you cannot move or rename this picture file.
That is this picture file is locked.
What if we wish that it will not be locked?
We can solve this problem be using FileStream, as below:
try { FileStream stream = new FileStream(openFileDialog1.FileName, FileMode.Open); var srcImage = Image.FromStream(stream); stream.Close(); myPictureBox.Image = srcImage; } catch (System.ArgumentException) { MessageBox.Show("Invalid picture format."); }
沒有留言:
張貼留言