C# 在VisualStudio中使用PictureBox

C# 在VisualStudio中使用PictureBox,c#,visual-studio,picturebox,C#,Visual Studio,Picturebox,我目前正在使用以下代码将图像加载到图片框中 pictureBox1.Image = Properties.Resources.Desert; 我将用“变量”替换“沙漠”,因为代码的工作方式如下 String Image_Name; Imgage_Name = "Desert"; pictureBox1.Image = Properties.Resources.Image_Name; 我有很多图片需要加载,我想用一个变量作为图片名,而不是为每个图片写一行。这可能吗 请查看以下内容:。默认情况下

我目前正在使用以下代码将图像加载到图片框中

pictureBox1.Image = Properties.Resources.Desert;
我将用“变量”替换“沙漠”,因为代码的工作方式如下

String Image_Name;
Imgage_Name = "Desert";
pictureBox1.Image = Properties.Resources.Image_Name;
我有很多图片需要加载,我想用一个变量作为图片名,而不是为每个图片写一行。这可能吗

请查看以下内容:。默认情况下,C#不允许您这样做。但是,您仍然可以使用
字符串
来访问所需的图像

您可以尝试以下方法:

Dictionary<string, Image> nameAndImg = new Dictionary<string, Image>()
{
    {"pic1",  Properties.Resources.pic1},
    {"pic2",  Properties.Resources.pic2}
    //and so on...
};

private void button1_Click(object sender, EventArgs e)
{
    string name = textBox1.Text;

    if (nameAndImg.ContainsKey(name))
        pictureBox1.Image = nameAndImg[name];

    else
        MessageBox.Show("Inavlid picture name");
}
using System.Collections;

string image_name = "Desert";

foreach (DictionaryEntry kvp in Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) {
    if ((string)kvp.Key == image_name) {
        var bmp = kvp.Value as Bitmap;
        if (bmp != null) {
            // bmp is your image
        }
    }
}
public Bitmap getResourceBitmapWithName(string image_name) {
    foreach (DictionaryEntry kvp in Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) {
        if ((string)kvp.Key == image_name) {
            var bmp = kvp.Value as Bitmap;
            if (bmp != null) {
                return bmp;
            }
        }
    }

    return null;
}
Dictionary nameAndImg=new Dictionary()
{
{“pic1”,Properties.Resources.pic1},
{“pic2”,Properties.Resources.pic2}
//等等。。。
};
私有无效按钮1\u单击(对象发送者,事件参数e)
{
字符串名称=textBox1.Text;
if(名称和mg.ContainsKey(名称))
pictureBox1.Image=nameAndImg[name];
其他的
MessageBox.Show(“Inavlid图片名称”);
}

您可以对资源进行迭代。。大概是这样的:

Dictionary<string, Image> nameAndImg = new Dictionary<string, Image>()
{
    {"pic1",  Properties.Resources.pic1},
    {"pic2",  Properties.Resources.pic2}
    //and so on...
};

private void button1_Click(object sender, EventArgs e)
{
    string name = textBox1.Text;

    if (nameAndImg.ContainsKey(name))
        pictureBox1.Image = nameAndImg[name];

    else
        MessageBox.Show("Inavlid picture name");
}
using System.Collections;

string image_name = "Desert";

foreach (DictionaryEntry kvp in Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) {
    if ((string)kvp.Key == image_name) {
        var bmp = kvp.Value as Bitmap;
        if (bmp != null) {
            // bmp is your image
        }
    }
}
public Bitmap getResourceBitmapWithName(string image_name) {
    foreach (DictionaryEntry kvp in Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) {
        if ((string)kvp.Key == image_name) {
            var bmp = kvp.Value as Bitmap;
            if (bmp != null) {
                return bmp;
            }
        }
    }

    return null;
}
你可以把它包装成一个漂亮的小函数。。大概是这样的:

Dictionary<string, Image> nameAndImg = new Dictionary<string, Image>()
{
    {"pic1",  Properties.Resources.pic1},
    {"pic2",  Properties.Resources.pic2}
    //and so on...
};

private void button1_Click(object sender, EventArgs e)
{
    string name = textBox1.Text;

    if (nameAndImg.ContainsKey(name))
        pictureBox1.Image = nameAndImg[name];

    else
        MessageBox.Show("Inavlid picture name");
}
using System.Collections;

string image_name = "Desert";

foreach (DictionaryEntry kvp in Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) {
    if ((string)kvp.Key == image_name) {
        var bmp = kvp.Value as Bitmap;
        if (bmp != null) {
            // bmp is your image
        }
    }
}
public Bitmap getResourceBitmapWithName(string image_name) {
    foreach (DictionaryEntry kvp in Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) {
        if ((string)kvp.Key == image_name) {
            var bmp = kvp.Value as Bitmap;
            if (bmp != null) {
                return bmp;
            }
        }
    }

    return null;
}
用法:

var resourceBitmap = getResourceBitmapWithName("Desert");
if (resourceBitmap != null) {
    pictureBox1.Image = resourceBitmap;
}

非常感谢你的努力。如果可以的话,“kvp”是什么?我将其命名为
kvp
,因为它代表一个“键值对”。字典上的条目。