Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
hi,

Is it possible to get Bitmap variable name as string?

for example, I have many Bitmap pictures:

C#
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:

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


now, foreach cycle runs throught all of them:

C#
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..
Posted
Updated 27-Apr-11 7:58am
v2

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;
  }
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 27-Apr-11 16:33pm    
That's definitely the OO way to do what OP wants! 5+
Sergey Alexandrovich Kryukov 27-Apr-11 23:55pm    
Not quite agree. Please see below, and my answer.
--SA
Kim Togo 28-Apr-11 2:44am    
Thanks Manfred
Sergey Alexandrovich Kryukov 27-Apr-11 23:54pm    
It helps, but not much. You anyway have a poorly maintainable piece of mapping between names and file names. It is not really needed.

The problem is deeper.
Please see my answer.
--SA
You can use a Dictionary[^] for that task.

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

Iterate over the KeyValuePairs:
C#
foreach (KeyValuePair<string,Bitmap> kvp in bitmapDictionary)
{
     if ( COMPARISON with my new BITMAP picture )
     {
         listBox1.Items.Add( " FOUND " + kvp.Key);
 
         break;
     }
}
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 27-Apr-11 23:56pm    
It helps, but not much. You anyway have a poorly maintainable piece of mapping between names and file names (where you add to a dictionary). It is not really needed.

The problem is deeper. Please see my answer.
--SA
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:

C#
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


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
 
Share this answer
 
Comments
Albin Abel 28-Apr-11 1:05am    
Well. I agree your mapping is good if the filenames some way match the index. But what I was discussing about different points. Whatever array or dictionary my point is same. As we need to dispose the bitmap exclusively and assume there are multiple references to a single bitmap object. The bitmap is disposed through one reference. Now the references are still not null, but it won't hold a bitmap. Then we access the bitmap through these references it throws exception. For example you are passing the bitmap to a function as argument.

private void myBitmapMetaInformationRetriver(Bitmap bmp)
{
...........
bmp.Dispose();
}

This function is little cautious function disposing the bitmap which it received. Now you call this function as myBitmapMetaInformationRetriver(bitmaps[0]);. Oh you lose the bitmap in your array. That is the point I was discussing about and I suggested keep the array of data instead of the bitmap object. My assumption might be theoretical, but just an information.

My 5 for the mapping idea and nice to meet you after a small gap :)
Sergey Alexandrovich Kryukov 28-Apr-11 1:15am    
Great to meet you two.
Thank you Albert. This mapping is just the illustration of the idea. In real life I would avoid the non-semantic names in a more radical way. The situation described would not even appear. There is a similar problem when many controls like edit boxes are added by Designer and all get some meaningful names (even when the developer is renaming them which really should be done). It's a good indicator of the case when those controls should be created by the code in some data-driven method. Data-driven approaches are invented a long ago but not really digested by the developers...
--SA
As RickShaub said dictionary is a good choice, you can retrieve the bitmap using the key

IDictionary<string,bitmap> imagelist=new Dictionary<string,bitmap>();

Then add your bitmaps like imagelist.Add("Bitmap1",bitmap1); imagelist.Add("Bitmap2",bitmap2); and retrieve like Bitmap bitmap=imagelist["Bitmap1"];.

All fine, but remember bitmaps must need to disposed after use.

An another issue if you call dispose the bitmap after added to the list the bitmap got disposed, but the reference of it still live in the list. it won't give negative if you check null and when access it you will get a fuzzy exceptions.

For example

XML
Bitmap bmp1 = new Bitmap(100, 100);
 IDictionary<string, Bitmap> list = new Dictionary<string, Bitmap>();
 list.Add("bitmap1",bmp1);
 bmp1.Dispose();
 Bitmap bmp2 = list["bitmap1"];
 if (bmp2 != null)
 {
     MessageBox.Show(bmp2.Width.ToString());
 }


Here the bmp2 is not null, but it is not holding the unmanaged object as well. bmp2.Width will throw exception in this case "parameter invalid" which is really a confusing exception. Just aware of it.

If the bitmaps are too heavy for the memory, keeping all those in a list would be undesirable.

I would recommend keeping those bitmap data in an array and convert to bitmap, use it, and dispose whenever necessary (if this is not going to be an iterative cycle). This is not a performance optimization, but to avoid memory leaks.

Just my view. Listen to other experts as well.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Apr-11 23:58pm    
I do not agree. I think this is artificial solution based on original misconception of the role of naming. You anyway have a poorly maintainable piece of mapping between names and file names. It is not really needed.

The problem is deeper. Please see my answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900