组合字符串和资源

本文关键字:资源 字符串 组合 | 更新日期: 2023-09-27 18:17:22

我有一个包含一些产品及其图像文件名(例如"test.png")的数据集。图像作为资源加载。如何正确设置图像位置?

DataRowView uRow = (DataRowView)comboBox1.SelectedItem;
DataRow row = uRow.Row;
pictureBox1.ImageLocation = Properties.Resources + row["logo"].ToString();

组合字符串和资源

您可以像这样通过名称获取字符串资源:

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);

如果您已经将图像本身作为资源存储,您可以像这样通过名称获取它们:

pictureBox1.Image =
    (Image)Properties.Resources.ResourceManager.GetObject(imageName);

ImageLocation属性不支持直接使用资源。来自MSDN文档:

获取或设置要在控件中显示的图像的路径或URL图片框。

要加载资源中的图像,请参见将PictureBox's的图像更改为我的资源中的图像?

如果设置资源的属性Access modifier。将FriendPublic
这样就可以访问图像或其他资源,而无需硬编码名称。
Visual Studio将为每个资源

生成一个类和属性
pictureBox1.Image = Properties.Resources.YourImage;