Load image from resources
Asked Answered
B

6

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?

Brunei answered 27/11, 2012 at 20:5 Comment(3)
What type of application are you working in? WinForms, WPF?Oppressive
@Oppressive I'd guess WinForms from the PictureBox tag.Polyp
Possible duplicate - #1192554Microscopium
S
54

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

Snaffle answered 27/11, 2012 at 20:17 Comment(2)
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.Classy
I like the notice mentioned in the end!Nonstandard
J
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;
}
Justice answered 27/11, 2012 at 20:17 Comment(0)
J
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);
Jaramillo answered 30/5, 2014 at 11:30 Comment(1)
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.Microscopium
O
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;
    }
Oppressive answered 27/11, 2012 at 20:31 Comment(0)
U
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);
Ultrasound answered 16/12, 2014 at 9:52 Comment(0)
I
-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;
Intracardiac answered 9/12, 2014 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.