【问题标题】:Load image from resources从资源加载图像
【发布时间】:2012-11-15 12:59: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;
    }
}

这样的事情可能吗?

【问题讨论】:

标签: c# image resources picturebox


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 非常感谢我所需要的。感谢您的解释。
    • @Loclip 很高兴能帮上忙。祝你有美好的一天:)
    • 注意:您需要“使用 x.y.z.Properties;”使这项工作。或者像 Wouter Huysentruit 的回答中的“Properties.Resources.ResourceManager.GetObject(channel)”。直接使用 System.Resources.ResourceManager.GetObject 是不行的。
    • 我喜欢最后提到的通知!
    【解决方案3】:

    如果您的图像位于资源文件中,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;
        }
    

    【讨论】:

      【解决方案4】:

      为 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 行转换为该行。
      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:
            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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-13
          • 1970-01-01
          • 2013-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-20
          相关资源
          最近更新 更多