tags:

views:

96

answers:

1

I am using a TreeView with an ImageList. As the TreeView is filled with items, each item will have an icon associated with it. Many of these icons could be identical, but currently, I am not adding any code to track duplicates, I simply add each icon to the ImageList and then track the icon associated with each TreeNode.

My question is about how .Net handles these resources. For example, does the runtime realize that some of the icons are exactly identical and thus only load the same icon once for all duplicates? If not, could I run into a resource issue if this is done tens of thousands of times (this won't be typical, but it could happen)?

+2  A: 

The Framework doesn't have any mechanism to handle resources like this as far as ensuring that you don't end up with duplicates loaded. This is particularly true in the case of a TreeView since the ImageList it uses maintains those images as resources local to the form which contains the TreeView.

An approach I have used in the past is to create a singleton object that wraps an ImageList. This allows you to control when/how an image gets added to the ImageList.

public sealed class ImageListManager
{
    private static volatile ImageListManager instance;
    private static object syncRoot = new object();
    private ImageList imageList;

    private ImageListManager()
    {
     this.imageList = new ImageList();
     this.imageList.ColorDepth = ColorDepth.Depth32Bit;
     this.imageList.TransparentColor = Color.Magenta;
    }

    /// <summary>
    /// Gets the <see cref="System.Windows.Forms.ImageList"/>.
    /// </summary>
    public ImageList ImageList
    {
     get
     {
      return this.imageList;
     }
    }

     /// <summary>
     /// Adds an image with the specified key to the end of the collection if it 
     /// doesn't already exist.
     /// </summary>
     public void AddImage(string imageKey, Image image)
     {
        if (!this.imageList.ContainsKey(imageKey))
        {
           this.imageList.Add(imageKey, image);
        }
     }

    /// <summary>
    /// Gets the current instance of the 
    /// <see cref="ImageListManager"/>.
    /// </summary>
    public static ImageListManager Instance
    {
     get
     {
      if (instance == null)
      {
       lock (syncRoot)
       {
        if (instance == null)
        {
         instance = new ImageListManager();
        }
       }
      }
      return instance;
     }
    }
}

You then give the TreeView a reference to that ImageList, generally done in the forms constructor after the call to InitializeComponent().

public partial class Form1 : Form
{
   public Form1()
   {
      InitializeComponent();
      this.treeView1.ImageList = ImageListManager.Instance.ImageList;
   }

   // Making some guesses about how you are adding nodes
   // and getting the associated image.
   public void AddNewTreeNode(string text, string imageKey, Image image)
   {
      TreeNode node = new TreeNode("display text");
      node.Name = "uniqueName";

      // This tells the new node to use the image in the TreeView.ImageList
      // that has imageKey as its key.
      node.ImageKey = imageKey;
      node.SelectedImageKey = imageKey;

      this.treeView1.Nodes.Add(node);

      // If the image doesn't already exist, this will add it.
      ImageListManager.Instance.AddImage(imageKey, image);
   }

}
Scott Dorman
Hi Scott, I either don't understand your answer or your answering a different question. My concern isn't multiple instances of the ImageList, but multiple copies of the same image assigned to multiple slots in the ImageList. For example, the identical image could be assigned to slots 0, 1, 2, 5, and 100.
Dennis
It sounded like you were concerned about having duplicated images across multiple TreeViews (or other controls) not having duplicated images in a single TreeView.ImageList. In either case, the .NET Framework does not provide any mechanism to manage these resources and pervent duplication. In the example in your comment, you will have 5 different copies of the image, one in each location in the ImageList. What you will need to do is track the duplicates yourself and if you detect that you are adding a node with an image that already exists, use the existing image index (or key).
Scott Dorman
Updated my answer, hopefuly this makes it clearer.
Scott Dorman
Thanks for the clarification Scott. That's pretty much what I assumed, so I have already been looking into alternative strategies.
Dennis