ImageList indexer returns Copy of Image instead of Reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?S2VuTg==?=

    ImageList indexer returns Copy of Image instead of Reference

    We're using a System.Windows. Forms.ImageList to store a bunch of Images that
    I've loaded, and rendering each image in the ImageList during a draw loop.
    The process was running unusually slow and was causing memory spikes. After
    several hours we discovered that the ImageList.Image s indexer is actually
    returning a copy of the added Images instead of simply returning a reference.
    Needless to say, we were a bit surprised and disturbed to discover this.

    1) Can anyone answer why the ImageList collection returns a copy instead of
    a reference?

    2) Is there a way to get the reference instead of a copy?

    If you run the example code below, the output to the console window will be
    "Different Image".

    // Example Code

    System.Windows. Forms.ImageList imageList =
    new System.Windows. Forms.ImageList ();

    System.Drawing. Image image =
    System.Drawing. Image.FromFile( @"C:\image.png" );

    imageList.Image s.Add(image);

    // Get the same Image twice!
    System.Drawing. Image imageFromList1 = imageList.Image s[0];
    System.Drawing. Image imageFromList2 = imageList.Image s[0];

    if (object.Referen ceEquals(imageF romList1, imageFromList2) )
    {
    Console.WriteLi ne("Same Image");
    }
    else
    {
    Console.WriteLi ne("Different Image");
    }

    Console.ReadLin e();
  • Marc Gravell

    #2
    Re: ImageList indexer returns Copy of Image instead of Reference

    Can you give more info on the setup? For example, would it be possible
    to simply switch to a "List<Image >"? or are you using the ImageList to
    supply other controls?

    Comment

    • =?Utf-8?B?S2VuTg==?=

      #3
      Re: ImageList indexer returns Copy of Image instead of Reference

      We're using a third party framework that is rendering the Image.
      Unfortunately, it requires the use of an ImageList, and an index into the
      ImageList. We currently have worked around the problem by not using this
      feature of the third party framework (using the List<Imagelike you
      suggested), but it's not an ideal solution.

      Thanks!

      "Marc Gravell" wrote:
      Can you give more info on the setup? For example, would it be possible
      to simply switch to a "List<Image >"? or are you using the ImageList to
      supply other controls?
      >

      Comment

      Working...