C#调用位图图像 [英] C# calling Bitmap image
问题描述
我是新手,我有问题,也许有人可以帮助我.
我有位图图像:
Hi,
I am beginner, and I have a problem, maybe someone can help me.
I have Bitmap images:
private static readonly Bitmap a1 = new Bitmap(WorkFolder4 + "a1.png");
private static readonly Bitmap a2 = new Bitmap(WorkFolder4 + "a2.png");
private static readonly Bitmap a3 = new Bitmap(WorkFolder4 + "a3.png");
//what I need is to call them using crafted variable name (methods first parameter is one of my images):
for (int k = 1; k < 20; k++)
{
string temp = "a" + Convert.ToString(k);
//at this time temp = "a1",....
method(temp);
}
请帮助,
Please Help,
推荐答案
尽管可以通过使用.NET System.Reflection API中的功能来实现.我求你不要这样做.通过将所有位图添加到数组或列表中,可以采用这种方法,您可以轻松地对其进行迭代:
Though this is possible by using functionality from the .NET System.Reflection API. I beg of you not to do this. Take this route instead by adding all Bitmaps to an array or a list and you can easily iterate over that:
private List<Bitmap> myBitmaps = new List<Bitmap>();
private static readonly Bitmap a1 = new Bitmap(WorkFolder4 + "a1.png");
myBitmap.Add(a1);
private static readonly Bitmap a2 = new Bitmap(WorkFolder4 + "a2.png");
myBitmap.Add(a2);
private static readonly Bitmap a3 = new Bitmap(WorkFolder4 + "a3.png");
myBitmap.Add(a3);
// Iterate over the Bitmaps in the list
foreach(Bitmap bm in myBitmaps)
{
method(bm);
}
您的方法为此课程或课程需要具有位图"类型的参数.您可能还会考虑使用图像路径的数组/列表,并在创建位图并调用方法的地方构建循环:
Your method needs to to have a parameter of type Bitmap for this or course. You might also think about having an array / List of the image paths and build a loop over that where you create the bitmaps and call your method:
private List<String> imagePaths = new List<String>();
imagePaths.Add(WorkFolder4 + "a1.png");
imagePaths.Add(WorkFolder4 + "a2.png");
imagePaths.Add(WorkFolder4 + "a3.png");
// iterate over the strings in your list to create and process the Bitmaps
foreach(String path in imagePaths)
{
Bitmap bm = new Bitmap(path);
method(bm);
}
除非您的方法方法"以某种方式存储或使用位图,否则必须将它们放入数组或列表中,以便以后仍可以访问它们. (WorkFolder4是否以反斜杠结尾?)
希望这可以帮助!如果您有任何疑问,请给我留言.
问候,
曼弗雷德(Manfred)
Unless your method "method" stores or uses the Bitmaps somehow you''d have to put them into an array or a list so you can still access them later. (Does WorkFolder4 end in a backslash?)
Hope this helps! If you have question leave me a comment.
Regards,
Manfred
这是您要找的吗?
Is this what you are looking for?
//what I need is to call them using crafted variable name (methods first parameter is one of my images):
for (int k = 1; k < 20; k++)
{
string temp = "a" + Convert.ToString(k);
//First way
Bitmap bitmap = new Bitmap(WorkFolder4 + temp);
method(temp);
//or Second Way
method( new Bitmap(WorkFolder4 + temp) );
}
这篇关于C#调用位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!