从资源中加载图像

33

我想要像这样加载图片:

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

这种事情可能吗?


你在开发哪种类型的应用程序?WinForms,WPF? - xr280xr
1
@xr280xr 我猜是从PictureBox标签中使用的WinForms。 - FThompson
可能是重复问题 - https://dev59.com/nHM_5IYBdhLWcg3w3nRs - vapcguy
6个回答

53
你可以始终使用 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

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

谢谢,
希望这对你有所帮助 :)


1
注意:您需要使用“using x.y.z.Properties;”才能使其正常工作。否则,就像Wouter Huysentruit的答案中所示,“Properties.Resources.ResourceManager.GetObject(channel)”一样。直接使用System.Resources.ResourceManager.GetObject将无法正常工作。 - Curtis Yallop
我喜欢最后提到的通知! - MCoder

11
您可以使用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;
}

8
尝试使用WPF进行此操作。
StreamResourceInfo sri = Application.GetResourceStream(new Uri("pack://application:,,,/WpfGifImage001;Component/Images/Progess_Green.gif"));
picBox1.Image = System.Drawing.Image.FromStream(sri.Stream);

太棒了!提示给用户,如果您只想要在VS的“Images”文件夹中的文件,请使用"pack://application:,,,/Images/your_file.gif"。如果您希望将其转换为Bitmap,则可以将FromStream行强制转换为该类型。 - vapcguy

4

如果您的图片在资源文件中,ResourceManager将起作用。如果它只是您项目中的一个文件(比如说位于根目录),您可以使用以下内容获取它:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = assembly .GetManifestResourceStream("AssemblyName." + channel);
this.pictureBox1.Image = Image.FromStream(file);

如果您在使用WPF:

    private ImageSource GetImage(string channel)
    {
        StreamResourceInfo sri = Application.GetResourceStream(new Uri("/TestApp;component/" + channel, UriKind.Relative));
        BitmapImage bmp = new BitmapImage();
        bmp.BeginInit();
        bmp.StreamSource = sri.Stream;
        bmp.EndInit();

        return bmp;
    }

0
    this.toolStrip1 = new System.Windows.Forms.ToolStrip();
    this.toolStrip1.Location = new System.Drawing.Point(0, 0);
    this.toolStrip1.Name = "toolStrip1";
    this.toolStrip1.Size = new System.Drawing.Size(444, 25);
    this.toolStrip1.TabIndex = 0;
    this.toolStrip1.Text = "toolStrip1";
    object O = global::WindowsFormsApplication1.Properties.Resources.ResourceManager.GetObject("best_robust_ghost");

    ToolStripButton btn = new ToolStripButton("m1");
    btn.DisplayStyle = ToolStripItemDisplayStyle.Image;
    btn.Image = (Image)O;
    this.toolStrip1.Items.Add(btn);

    this.Controls.Add(this.toolStrip1);

-2

您可以在项目中添加图像资源,然后(右键单击项目并选择属性项)以以下方式访问:

this.picturebox.image = projectname.properties.resources.imagename;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接