简体   繁体   中英

Retrieve image and display in picturebox with image path stored in database c#

I have Image Paths stored in my database under a column called Image in the format of Images\\ac.jpg The path is already stored in a textbox called txtImage.Text . I am trying to display it in a picturebox control but I get the following error:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll

Additional information: Images\\ac.jpg

pbImage.Image = System.Drawing.Image.FromFile(txtImage.Text);

You need to provide the full path to your image to the PictureBox . The relative path Images\\ac.jpg will not work. You need an absolute path like C:\\Images\\ac.jpg

If your Images folder is where your executable is you can try creating the absolute path as shown below

string absolutePath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), @"Images\ac.jpg");

pbCoffeeImage.Image = System.Drawing.Image.FromFile(absolutePath);

If you are going to allow the user to input the image path, I would recommend you use the FileDialog control to make sure the path is valid and an absolute path.

Images\\ac.jpg is a relative path which is resolved relative to the current working path of your application. The error tells you, that at this location ( workingPath\\Images\\ac.jpg ) no file exists. I'd convert it to an absolute path, by adding the path to your Images-folder at the beginning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM