【问题标题】:Combining a string and resource组合字符串和资源
【发布时间】:2015-10-12 09:40:58
【问题描述】:

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

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

pictureBox1.ImageLocation = Properties.Resources + row["logo"].ToString();

【问题讨论】:

  • 请使用适当的 GUI 技术进行标记:Webforms?赢表格? WPF?
  • 对不起,winforms是这样

标签: c# winforms


【解决方案1】:

你可以像这样按名称获取字符串资源:

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

【讨论】:

  • 听起来图像本身是资源,而不是路径,但这仍然是 OP 需要的正确提示。
  • 嗯,他把结果放到ImageLocation,所以我不确定。
【解决方案2】:

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

获取或设置要在 图片框。

要加载作为资源的图像,请参阅Change PictureBox's image to image from my resources?

【讨论】:

    【解决方案3】:

    如果将 Resources.resx 文件的属性 Access modifier 设置为 FriendPublic
    然后无需硬编码名称即可访问图像或其他资源。
    Visual Studio 将为每个资源生成一个类和属性

    pictureBox1.Image = Properties.Resources.YourImage;
    

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      相关资源
      最近更新 更多