469,621 Members | 1,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

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.Images 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.Images.Add(image);

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

if (object.ReferenceEquals(imageFromList1, imageFromList2))
{
Console.WriteLine("Same Image");
}
else
{
Console.WriteLine("Different Image");
}

Console.ReadLine();
Feb 21 '08 #1
2 3305
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?
Feb 21 '08 #2
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?
Feb 21 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Nikhil Patel | last post: by
1 post views Thread by Iulian Ionescu | last post: by
16 posts views Thread by Roger | last post: by
1 post views Thread by Peter Larsen | last post: by
2 posts views Thread by senfo | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.