0

I have a dataset which contains some products and their image filenames (for example "test.png"). Images are loaded as resources. How can I set the image location properly?

DataRowView uRow = (DataRowView)comboBox1.SelectedItem;
DataRow row = uRow.Row;

pictureBox1.ImageLocation = Properties.Resources + row["logo"].ToString();
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Senpai
  • 515
  • 6
  • 18

3 Answers3

1

You can get a string resource by name like this:

string imageName = row["logo"].ToString();

// Strip off the extension, since it is not contained in the resource name.
imageName = Path.GetFileNameWithoutExtension(imageName);

pictureBox1.ImageLocation =
    Properties.Resources.ResourceManager.GetString(imageName);

If you have stored the images themselves as resources, you can get them by name like this:

pictureBox1.Image =
    (Image)Properties.Resources.ResourceManager.GetObject(imageName);
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

The ImageLocation property does not support using resources directly. From the MSDN Docs:

Gets or sets the path or URL for the image to display in the PictureBox.

To load an image that is a resource, please see Change PictureBox's image to image from my resources?

Community
  • 1
  • 1
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
0

If you set property Access modifier of Resources.resx file to the Friend or Public
then images or another resources can be accessed without hardcoding names.
Visual Studio will generate a class and properties for every resources

pictureBox1.Image = Properties.Resources.YourImage;
Fabio
  • 31,528
  • 4
  • 33
  • 72