我正在尝试从本地磁盘加载图像,它正在工作.但我的问题是,我想检查文件夹中是否有图像,如果没有 - 那么MessageBox.Show("No image!");
载入图片:
Bitmap bitmap1 = new Bitmap(@"Documentation\\Pictures\\"+table[8]+".jpg");
pictureBox.Image=bitmap1;
Run Code Online (Sandbox Code Playgroud)
您可以使用File.Exists方法检查给定文件是否存在:
var file = Path.ChangeExtension(table[8], ".jpg");
var fullPath = Path.Combine(@"Documentation\Pictures", file);
if (!File.Exists(fullPath))
{
MessageBox.Show("No image!");
}
else
{
pictureBox.Image = new Bitmap(fullPath);
}
Run Code Online (Sandbox Code Playgroud)