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();
2
  • 1
    Please tag with appropriate GUI technology: Webforms? WinForms? WPF? Commented Oct 11, 2015 at 19:23
  • I'm sorry, winforms it is Commented Oct 11, 2015 at 19:27

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

It sounds like the Image itself is the resource, not the path, but this is still the right hint the OP needs.
Well, he puts the result into ImageLocation, so I'm not sure.
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?

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.