简体   繁体   中英

Images in Picturebox c#

I'm writing a program about trying different images in picturebox, but the image must correspond to the text. Plus it must come from "Resources" folder of the project.

this is what i want to do if the text "apple" displays to the screen then the image with a filename "apple" would also display in the picturebox..

I can do it in "if-else" like this

string word="apple";
if(word==apple)
pictureBox1.Image=  WindowsFormsApplication4.Properties.Resources.apple;

but What if I have a thousand images, I still think there's an easy way for this,..

i'm trying this one,,

string word=label1.Text;//label1.text changes from time to time
pictureBox1.Image= WindowsFormsApplication4.Properties.Resources.word;

but I know "word" is string....It's not possible...I cannot append string to the syntax....

You can pass in a string using the GetObject method of the ResourceManager class:

string itemName = label1.Text;
this.pictureBox1.Image = 
    (Image)Properties.Resources.ResourceManager.GetObject(itemName);

If you open resources .Designer.cs file code, you will see something like:

internal static System.Drawing.Bitmap apple {
    get {
        object obj = ResourceManager.GetObject("apple", resourceCulture);
        return ((System.Drawing.Bitmap)(obj));
    }
}

so you can do the same thing:

string word=label1.Text;//label1.text changes from time to time
pictureBox1.Image= (System.Drawing.Bitmap)WindowsFormsApplication4
                                           .Properties
                                           .Resources
                                           .ResourceManager.GetObject(word);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM