33

I want to load the image like this:

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

Because I don't want to do

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?

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
a1204773
  • 6,923
  • 20
  • 64
  • 94

6 Answers6

53

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

Example

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

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 :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • 1
    Note: You need the "using x.y.z.Properties;" to make this work. Or else "Properties.Resources.ResourceManager.GetObject(channel)" like in Wouter Huysentruit's answer. Using System.Resources.ResourceManager.GetObject directly will not work. – Curtis Yallop Mar 12 '14 at 22:40
  • I like the notice mentioned in the end! – MCoder Feb 20 '20 at 20:50
11

You can do this using the 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;
}
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
8

Try this for WPF

StreamResourceInfo sri = Application.GetResourceStream(new Uri("pack://application:,,,/WpfGifImage001;Component/Images/Progess_Green.gif"));
picBox1.Image = System.Drawing.Image.FromStream(sri.Stream);
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
Roopsundar
  • 81
  • 1
  • 2
  • Awesome! Note to users, just do `"pack://application:,,,/Images/your_file.gif"` if you just want a file that's in your `Images` folder in VS. You can also return it to a `Bitmap` if you just cast the `FromStream` line to that, if you so desire. – vapcguy Mar 22 '17 at 21:33
4

ResourceManager will work if your image is in a resource file. If it is just a file in your project (let's say the root) you can get it using something like this:

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

Or if you're in 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;
    }
xr280xr
  • 12,621
  • 7
  • 81
  • 125
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);
IR.Programmer
  • 119
  • 3
  • 4
-2

You can add an image resource in the project then (right click on the project and choose the Properties item) access that in this way:

this.picturebox.image = projectname.properties.resources.imagename;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HamidReza
  • 1,726
  • 20
  • 15