开发者

Loading and checking image from local disk in C#

开发者 https://www.devze.com 2023-02-23 03:29 出处:网络
i\'m trying to load image from local disk, and it\'s working. But my problem is that i\'d like to check if image is available in folder, and if not - then MessageBox.Show(\"No image!\");

i'm trying to load image from local disk, and it's working. But my problem is that i'd like to check if image is available in folder, and if not - then MessageBox.Show("No image!");

Loading image:

 Bitmap bitmap1 = new Bitmap(@"Documentation\\Pictures\\"+table[8]+".jpg");
 pic开发者_运维问答tureBox.Image=bitmap1;


You could use the File.Exists method to check whether a given 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);
}


Try using the File.Exists method for testing if the file itself exists. Note, however, that between invoking that method and invoking the method which actually loads the file, the file could already be gone. Thus, exception handling should be used nonetheless.

See this link for further info.


Try this

string fileName = string.Format(@"Documentation\\Pictures\\{0}.jpg",table[8]);
if(!File.Exists(fileName))
{
MessageBox.Show("No Image");
}
else
{
Picture1.Image = Image.FromFile(fileName);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消