从资源加载图像

问题描述:

我要加载的形象是这样的:

I want to load the image like this:

void info(string channel)
{
    //Something like that
    channelPic.Image = Properties.Resources.+channel
}

由于我不想做

void info(string channel)
{
    switch(channel)
    {
        case "chan1":
            channelPic.Image = Properties.Resources.chan1;
            break;
        case "chan2":
            channelPic.Image = Properties.Resources.chan2;
            break;
    }
}

是这样的可能吗?

Is something like this possible?

您可以随时使用 System.Resources.ResourceManager 返回缓存的ResourceManager 此类使用。由于 CHAN1 CHAN2 重新present两个不同的图像,您可以使用系统。 Resources.ResourceManager.GetObject(字符串名称)返回一个对象,你的投入与项目资源匹配

You can always use System.Resources.ResourceManager which returns the cached ResourceManager used by this class. Since chan1 and chan2 represent two different images, you may use System.Resources.ResourceManager.GetObject(string name) which returns an object matching your input with the project resources

示例

object O = Resources.ResourceManager.GetObject("chan1"); //Return an object from the image chan1.png in the project
channelPic.Image = (Image)O; //Set the Image property of channelPic to the returned object as Image

的通知 Resources.ResourceManager.GetObject(字符串名称)可以返回如果指定字符串中的项目资源没有被发现。

Notice: Resources.ResourceManager.GetObject(string name) may return null if the string specified was not found in the project resources.

谢谢,
结果我希望对您有所帮助:)

Thanks,
I hope you find this helpful :)