在foreach循环使用字符串变量检索图像资源 [英] Retrieve image resource using string variable in foreach loop
问题描述
我有一个字符串数组ABC
我把这个在每个循环。我要检索从foreach循环利用的价值资源的图像,并把它变成一个图片框
I have a string array "abc" I put this in a for each loop. I want to retrieve an image from resources using the value in the foreach loop and put it into a picture box.
下面的代码:
char[] stringArr = inputted.ToCharArray();
foreach (char i in stringArr)
{
PictureBox pictureBox = new PictureBox();
object obj = ResourceManager.GetObject(i.ToString());
pictureBox.Image = ((System.Drawing.Bitmap)(obj));
Controls.Add(pictureBox);
}
我有什么做的,得到这个工作?
我试图来实现的,是一个有图片,字母表中的每个字符都代表不同的画面,用户输入的字符串,并点击一个按钮,用户输入取,形成了stringArr,我想它输出的基础上,他输入字符串
What do i have to do to get this working? What i am trying to achieve, is a have pictures, each character in the alphabet represents a different picture, the user inputs a string and clicks a button, the users inputs is taken, formed to the stringArr and i want it to output the relevant images based on the string he inputted
推荐答案
您可以做类似的相关图片:
You could do something like:
object obj = ResourceManager.GetObject("MyResourceName", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
要获得通过名称的资源。
To get a resource by name.
在的ResourceManager
是这样的:
var ResourceManager =
new System.Resources.ResourceManager(
"YourAssembly.Properties.Resources",
typeof(Resources).Assembly);
因此,在您的例子,你可以写:
So in your example you could write:
foreach (char i in stringArr)
{
PictureBox pictureBox = new PictureBox();
object obj = ResourceManager.GetObject(i.ToString(), resourceCulture);
pictureBox.Image = ((System.Drawing.Bitmap)(obj));
}
(你也可以省略 resourceCulture
参数,如果你的形象是没有什么特别的文化)。
(You also could omit the resourceCulture
parameter if your image is of no special culture).
我不认为你的代码只是从一个更大的示例的摘录,因为它是没有意义的我创建一个图片框
一看里面的不的将其分配到的一种形式。
I do assume that your code is just an excerpt from a larger example since it makes no sense to me to create a PictureBox
inside a look and not assign it to a form.
这篇关于在foreach循环使用字符串变量检索图像资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!