从资源加载图像

a12*_*773 29 c# resources image picturebox

我想加载这样的图像:

void info(string channel)
{
    //Something like that
    channelPic.Image = Properties.Resources.+channel
}
Run Code Online (Sandbox Code Playgroud)

因为我不想这样做

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

这样的事情可能吗?

Pic*_*are 48

您始终可以使用System.Resources.ResourceManager它返回ResourceManager此类使用的缓存.由于chan1chan2代表两个不同的图像,您可以使用System.Resources.ResourceManager.GetObject(string name)它返回与您的输入匹配的对象与项目资源

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
Run Code Online (Sandbox Code Playgroud)

注意:如果在项目资源中找不到指定的字符串,则Resources.ResourceManager.GetObject(string name)可能会返回null.

谢谢,
我希望你觉得这很有帮助:)


huy*_*itw 10

你可以这样做ResourceManager:

public bool info(string channel)
{
   object o = Properties.Resources.ResourceManager.GetObject(channel);
   if (o is Image)
   {
       channelPic.Image = o as Image;
       return true;
   }
   return false;
}
Run Code Online (Sandbox Code Playgroud)


小智 6

试试这个WPF吧

StreamResourceInfo sri = Application.GetResourceStream(new Uri("pack://application:,,,/WpfGifImage001;Component/Images/Progess_Green.gif"));
picBox1.Image = System.Drawing.Image.FromStream(sri.Stream);
Run Code Online (Sandbox Code Playgroud)