是否可以获取位图变量名称? [英] Is it possible to get Bitmap variable name?

查看:58
本文介绍了是否可以获取位图变量名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



是否可以将Bitmap变量名称获取为字符串?

例如,我有很多位图图片:



Is it possible to get Bitmap variable name as string?

for example, I have many Bitmap pictures:

private static readonly Bitmap a11 = new Bitmap(WorkFolder + "a11.png");
private static readonly Bitmap a12 = new Bitmap(WorkFolder + "a12.png");
private static readonly Bitmap a13 = new Bitmap(WorkFolder + "a13.png");
private static readonly Bitmap a14 = new Bitmap(WorkFolder + "a14.png");
private static readonly Bitmap a15 = new Bitmap(WorkFolder + "a15.png");


...
我将它们全部添加到列表中:


...
I added them all to the list:

MyBitmaps.AddRange(new Bitmap[]{a11,a12,a13,a14,a15,a16,..,...});



现在,foreach循环贯穿所有这些循环:



now, foreach cycle runs throught all of them:

foreach (var bitamp in MyBitmaps)
{
 if ( COMPARISON with my new BITMAP picture )
  {
  listBox1.Items.Add( " FOUND ");

//**************
    Here I need to write bitamps variable name to the string array (for example: "a11" or "a12")
//**************
  break;
  }



我不知道该怎么做..



I have no Ideas how to do that..

推荐答案

您可以创建一个新类,其中包含您的位图和元数据

You can make a new class that holds your Bitmap and meta data

public class MyBitmap
{
  public Bitmap Picture;
  public string Name;

  public MyBitmap(Bitmap picture, string name)
  {
    this.Picture = picture;
    this.Name = name;
  }
}

private static readonly MyBitmap a11 = new MyBitmap(new Bitmap(WorkFolder + "a11.png"), "Peter");
private static readonly MyBitmap a12 = new MyBitmap(new Bitmap(WorkFolder + "a12.png"), "Allan");

MyBitmaps.AddRange(new MyBitmap[]{a11,a12,a13,a14,a15,a16,..,...});





foreach (var myBitmap in MyBitmaps)
{
  if ( COMPARISON with my new BITMAP picture )
  {
    listBox1.Items.Add( " FOUND " + myBitmap.Name);
    break;
  }


您可以使用词典 [ ^ ].

添加位图:
You can use a Dictionary[^] for that task.

To add your Bitmaps:
Dictionary<string,Bitmap> bitmapDictionary = new Dictionary<string,Bitmap>();
bitmapDictionary.Add("a11.png", a11);
bitmapDictionary.Add("a12.png", a12);
...etc...


遍历KeyValuePairs:


Iterate over the KeyValuePairs:

foreach (KeyValuePair<string,Bitmap> kvp in bitmapDictionary)
{
     if ( COMPARISON with my new BITMAP picture )
     {
         listBox1.Items.Add( " FOUND " + kvp.Key);
 
         break;
     }
}


问题更加严重.这实际上是关于不要重复自己"原理的命名( http://en.wikipedia.org/wiki/Do_not_repeat_yourself [ ^ ]).您尝试在一对一的对应映射中应用两组名称:一组名称是文件名,另一组是变量名.

无论您做什么,都将获得有限的成功.从至少可维护性的角度来看,应该至少在一个地方构建映射(例如,使用字典),这是一个痛苦的地方.问题是您真的不需要为每个对象使用单独的名称.

正确的解决方案不应该从对象标识开始,而应该从您要在语义上使用名称的位置开始.应避免名称不带有任何语义.由于未针对此应用程序执行此想法,因此适当的解决方案可能会有所不同.

这是这个想法的一些例证.假设您在目录中有几个位图,并且如果00.png01.png02.png则命名,请执行以下操作:

The problem is deeper. It is really about the naming in application of the Don''t Repeat Yourself principle (http://en.wikipedia.org/wiki/Do_not_repeat_yourself[^]). Your trying to apply two sets of names in one-to-one corresponding mapping: one set of names is file names, another one is the set of the variable names.

Whatever you do with this will always be of limited success. There should be at least one place where you build the mapping (using a dictionary, for example) which would be a painful point from the standpoint of maintainability. The problem is that you really don''t need a separate name for each object.

The right solution should start not with object identification but with the place where you want to use the name semantically. The names not carrying any semantics should be avoided. As this idea is not conducted for this application, an adequate solutions can be different.

Here is some illustration of the idea. Let''s assume you have several bitmaps in the directory and name if 00.png, 01.png, 02.png… Let''s do the following:

string dataDirectory = //...

//...

string[] fileNames = System.IO.Directory.GetFiles(dataDirectory);
System.Drawing.Bitmap[] bitmaps = new System.Drawing.Bitmap[fileNames.Length];
for (int index = 0; index < fileNames.Length; ++index)
   bitmaps[index] = new System.Drawing.Bitmap(fileNames[index]);

//...

System.Drawing.Bitmap someBitmap = bitmaps[04]; //from 04.png



在此示例中,位图的名称"始终是其对应于文件的索引.这只是这个想法的一个例证.在编程中,永远不需要包含数字值的任何对象的名称.

—SA



In this example, the "name" of the bitmap is always its index corresponding to a file. This is just an illustration of the idea. In programming, the names of any objects containing numeric value are never needed.

—SA


这篇关于是否可以获取位图变量名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆