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.
t开发者_如何学Chis 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);
精彩评论