Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am loading a picture from a file on my drive. After this, I click a button to add this file to a list and show it in a listbox. The code for adding the file to list looks like:
C#
listbox1.Items.Clear();
mainlibrary.AddPicture((Bitmap)Image.FromFile(picfilename)); //method adds loaded file to list
foreach (Bitmap item in mainlibrary.GetPictureList()) //gets all items in list
{
  listbox1.Items.Add(item.ToString());
}

The problem I'm having is that the code returns "System.Drawing.Bitmap" to listbox1, but I want to see the name of the bitmap file instead like "banana.jpg". How can I achieve this or what am I doing wrong?

Thank you.
Posted

A Bitmap object has no property that associates it with the file it may have been loaded from. You could create a new class that inherits Bitmap and add this extra property.
 
Share this answer
 
A bitmap doesn't "have" a file name - loading it from a file is not the only way to create one. In fact, Image.FromFile uses native windows calls to load the file - which creates a stream and reads it from that.

If you want to keep a file name with the bitmap data, you will have to create your own class (possibly derived from Image or Bitmap) which adds the extra information.
 
Share this answer
 
Comments
Member 11971544 3-Nov-15 4:58am    
Is that the same with the Image class?
OriginalGriff 3-Nov-15 5:02am    
Yes - Image is the abstract base class from which Bitmap is derived, so it contains even less information!

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