首页 > 解决方案 > Visual C# 从资源加载图像

问题描述

我似乎无法从我的资源文件夹中加载图像,图像会根据组合框的值而变化

if(cmbProduct.Text == "T-Shirt, Black, The Big Day Out" || 
   cmbProduct.Text == "T-Shirt, Black, V-Vestival" || 
   cmbProduct.Text == "T-Shirt, Black, Sound Relief")
{
    picProduct.Visible = true;
    picProduct.Image = Image.FromFile("T-Shirt, Black.jpeg");
}

标签: c#

解决方案


@Felix D 提到的是将您的图像作为“现有文件”添加到资源中。不过,“ T-Shirt, Black.jpeg ”的名称将T_Shirt__Black在资源中重命名为。

我相信以下是您可能想要实现的目标:-

private void cmbProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            picProduct.Visible = false;
            picProduct.InitialImage = null;
            if (cmbProduct.Text == "T-Shirt, Black, The Big Day Out" || cmbProduct.Text == "T-Shirt, Black, V-Vestival" || cmbProduct.Text == "T-Shirt, Black, Sound Relief")
            {
                picProduct.Visible = true;
                picProduct.Image = Properties.Resources.T_Shirt__Black;
            }
        }

在解决方案资源管理器中,右键单击您的projectName --> Properties --> Add Existing File... 并选择所需的文件。在插入时,Visual Studio 将自动重命名资源图像,用下划线替换空格、破折号、逗号,因此当您编写 时Properties.Resources.,IntelliSense 将显示属性具有的内容。

在此处输入图像描述

在此处输入图像描述


推荐阅读