Search code examples
vb.netpicturebox

VB.Net Picture Box Control


OK so currently this is the code I have below of a picture box

PxBx.Image = Image.FromFile("C:\Users\Ashleysaurus\Documents\Visual Studio 2015\Projects\ThisProject\ThisProject\Images\filename.JPG")

What is the correct syntax for using images files stored in the application files after i publish a project? Namely b/c this path will not always be the same based off when/where users would store the app files?

I did a bit of searching but havent figured out how to ask the correct question to get the answer from forums/google.

Thanks in advance


Solution

  • I suggest you to add to your Settings.settings a new entry for the path to the folder where you store the images. This setting will be read at runtime and used to find your images. The advantage of this approach is that you can manually (or through your setup code) change the config file (where the setting is stored) to whatever your customer requires. Instead, hard coding a path (relative or not) inside the code is just a problem waiting to happen.

    To add an entry in your setting.settings file right click your project and select properties, then the settings tab. Here, add a Name like "ImagePath", of Type = String and Scope = "Application", then set your current path as value and save.

    Now, if you open the app.config (or web.config) file you will see the new entry and its value. To use it at runtime

    string imagePath = Properties.Settings.Default.ImagePath;