繁体   English   中英

Picturebox c# 中的图像

[英]Images in Picturebox c#

我正在编写一个关于在图片框中尝试不同图像的程序,但图像必须与文本相对应。 另外,它必须来自项目的“资源”文件夹。

如果文本“apple”显示在屏幕上,这就是我想要做的,那么文件名“apple”的图像也将显示在图片框中..

我可以像这样在“if-else”中做到这一点

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

但是如果我有一千张图片怎么办,我仍然认为有一个简单的方法,..

我正在尝试这个,,

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

但我知道“单词”是字符串......这是不可能的......我不能 append 字符串到语法......

您可以使用 ResourceManager class 的GetObject方法传入一个字符串:

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

如果您打开资源.Designer.cs文件代码,您将看到如下内容:

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

所以你可以做同样的事情:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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