Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image from resources

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?

like image 635
a1204773 Avatar asked Nov 27 '12 20:11

a1204773


People also ask

How do I import a resource image into unity?

Load SpriteFirst convert the image into sprite in unity editor, then you can load that at runtime just like textures. In the scene, you need to use “Image” game object instead of “RawImage” game object to load the sprite. Add below given script to Main Camera or any other game object. Run the application.

How do I add an image to a resource?

Add Image ResourceIn Visual Studio, click the Project menu, and select Add Existing Item. Find and select the image you want to add to your project. In the Solution Explorer window, right-click the image file you just added to your project, and select Properties from the popup menu.


2 Answers

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

like image 99
Picrofo Software Avatar answered Oct 24 '22 17:10

Picrofo Software


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; } 
like image 45
huysentruitw Avatar answered Oct 24 '22 18:10

huysentruitw