ImageList Strange Behavior
Messages   Related Types
This message was discovered on ASPFriends.com 'winforms-cs' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Dan Hurwitz (VIP)
(Sorry for the duplicate post. I first sent it accidentally without a
title.)
=20
I have a small WinForms program which adds several images to an
ImageList. The form has several controls which then display one of the
images, with a NumericUpDown control to select which image to display on
the controls. One of the images in the original ImageList is replaced
by a different image.
=20
This all works fine, in both C# and VB.NET. The relevant snippet of
code from the C# version is shown below. I have reproduced the exact
same behavior in Visual Studio.NET as well as in a hand coded and
compiled version.
=20
The problem arises when I change the ImageSize property. When the lines
of code to change the ImageSize are added to the program, the
replacement image - wrench.ico - is superimposed on rocket.ico, rather
than replacing it. When the ImageSize property is not changed, the
wrench replaces the rocket as it should.
=20
Any suggestions? Thanks in advance.
=20
--d
************************************
=20
imgList =3D new ImageList();
Image img;
=20
// Use an array to add filenames to the ImageList
String[] arFiles =3D {
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +=20
@"Graphics\bitmaps\assorted\happy.bmp",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +=20
@"Graphics\bitmaps\assorted\hand.bmp",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +=20
@"Graphics\bitmaps\assorted\phone.bmp",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +=20
@"Graphics\icons\industry\bicycle.ico",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +=20
@"Graphics\icons\industry\hammer.ico",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +=20
@"Graphics\icons\industry\rocket.ico"
};

for (int i =3D 0; i < arFiles.Length; i++)
{
img =3D Image.FromFile(arFiles[i]);
imgList.Images.Add(img);
}
=20
// Change the size
imgList.ImageSize =3D new Size(32, 32);
=20
// Replace an image
img =3D Image.FromFile(
"C:\\Program Files\\Microsoft Visual Studio .NET\\Common7\\" +=20
"Graphics\\icons\\industry\\wrench.ico");
imgList.Images[imgList.Images.Count - 1] =3D img;

-------------------------------------------------------------------
Dan Hurwitz voice: 781-275-3375
Sterling Solutions fax: 781-275-3846
3 Liljegren Way Click here to reveal e-mail address
<mailto:Click here to reveal e-mail address> =20
Bedford, MA 01730 www.stersol.com
<http://www.stersol.com/> =20

Co-author with Jesse Liberty: Programming ASP.NET
<http://www.oreilly.com/catalog/progaspdotnet/> (O'Reilly)
------------------------------------------------------------------ =20
=20

Reply to this message...
 
    
Stephen Rees
Dan ...

I've never done this and it's hard to see without the rest of the code. But
I took a quick peek at
Microsoft.NET\FrameworkSDK\Samples\QuickStart\winforms\samples\controlrefere
nce\listboxctl\cs and they seem to clear the image before replacing it.

It's only a guess but with images of the same size the top one will always
obscure the bottom one and make it look like it has replaced it. If the
bottom image is bigger then it's going to be visible underneath on the
edges.

Try clearing the image before replacing or resizing. It's good practice to
be clean anyway ... even if we have got garbage collection with dotNET.

I'd say it would be a good idea to use the image panel in the above example
also.

Steve.

-----Original Message-----
From: Dan Hurwitz [mailto:Click here to reveal e-mail address]
Sent: 14 June 2002 01:32
To: winforms-cs
Subject: [winforms-cs] ImageList Strange Behavior

(Sorry for the duplicate post. I first sent it accidentally without a
title.)

I have a small WinForms program which adds several images to an
ImageList. The form has several controls which then display one of the
images, with a NumericUpDown control to select which image to display on
the controls. One of the images in the original ImageList is replaced
by a different image.

This all works fine, in both C# and VB.NET. The relevant snippet of
code from the C# version is shown below. I have reproduced the exact
same behavior in Visual Studio.NET as well as in a hand coded and
compiled version.

The problem arises when I change the ImageSize property. When the lines
of code to change the ImageSize are added to the program, the
replacement image - wrench.ico - is superimposed on rocket.ico, rather
than replacing it. When the ImageSize property is not changed, the
wrench replaces the rocket as it should.

Any suggestions? Thanks in advance.

--d
************************************

imgList = new ImageList();
Image img;

// Use an array to add filenames to the ImageList
String[] arFiles = {
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\bitmaps\assorted\happy.bmp",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\bitmaps\assorted\hand.bmp",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\bitmaps\assorted\phone.bmp",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\icons\industry\bicycle.ico",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\icons\industry\hammer.ico",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\icons\industry\rocket.ico"
};

for (int i = 0; i < arFiles.Length; i++)
{
img = Image.FromFile(arFiles[i]);
imgList.Images.Add(img);
}

// Change the size
imgList.ImageSize = new Size(32, 32);

// Replace an image
img = Image.FromFile(
"C:\\Program Files\\Microsoft Visual Studio .NET\\Common7\\" +
"Graphics\\icons\\industry\\wrench.ico");
imgList.Images[imgList.Images.Count - 1] = img;

-------------------------------------------------------------------
Dan Hurwitz voice: 781-275-3375
Sterling Solutions fax: 781-275-3846
3 Liljegren Way Click here to reveal e-mail address
<mailto:Click here to reveal e-mail address>
Bedford, MA 01730 www.stersol.com
<http://www.stersol.com/>

Co-author with Jesse Liberty: Programming ASP.NET
<http://www.oreilly.com/catalog/progaspdotnet/> (O'Reilly)
------------------------------------------------------------------

| [winforms-cs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/winforms-cs.asp = JOIN/QUIT

Reply to this message...
 
    
Marisk
Try an invalidate and repaint.

----- Original Message -----
From: "Dan Hurwitz" <Click here to reveal e-mail address>
To: "winforms-cs" <Click here to reveal e-mail address>
Sent: Thursday, June 13, 2002 5:31 PM
Subject: [winforms-cs] ImageList Strange Behavior

(Sorry for the duplicate post. I first sent it accidentally without a
title.)

I have a small WinForms program which adds several images to an
ImageList. The form has several controls which then display one of the
images, with a NumericUpDown control to select which image to display on
the controls. One of the images in the original ImageList is replaced
by a different image.

This all works fine, in both C# and VB.NET. The relevant snippet of
code from the C# version is shown below. I have reproduced the exact
same behavior in Visual Studio.NET as well as in a hand coded and
compiled version.

The problem arises when I change the ImageSize property. When the lines
of code to change the ImageSize are added to the program, the
replacement image - wrench.ico - is superimposed on rocket.ico, rather
than replacing it. When the ImageSize property is not changed, the
wrench replaces the rocket as it should.

Any suggestions? Thanks in advance.

--d
************************************

imgList = new ImageList();
Image img;

// Use an array to add filenames to the ImageList
String[] arFiles = {
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\bitmaps\assorted\happy.bmp",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\bitmaps\assorted\hand.bmp",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\bitmaps\assorted\phone.bmp",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\icons\industry\bicycle.ico",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\icons\industry\hammer.ico",
@"C:\Program Files\Microsoft Visual Studio .NET\Common7\" +
@"Graphics\icons\industry\rocket.ico"
};

for (int i = 0; i < arFiles.Length; i++)
{
img = Image.FromFile(arFiles[i]);
imgList.Images.Add(img);
}

// Change the size
imgList.ImageSize = new Size(32, 32);

// Replace an image
img = Image.FromFile(
"C:\\Program Files\\Microsoft Visual Studio .NET\\Common7\\" +
"Graphics\\icons\\industry\\wrench.ico");
imgList.Images[imgList.Images.Count - 1] = img;

-------------------------------------------------------------------
Dan Hurwitz voice: 781-275-3375
Sterling Solutions fax: 781-275-3846
3 Liljegren Way Click here to reveal e-mail address
<mailto:Click here to reveal e-mail address>
Bedford, MA 01730 www.stersol.com
<http://www.stersol.com/>

Co-author with Jesse Liberty: Programming ASP.NET
<http://www.oreilly.com/catalog/progaspdotnet/> (O'Reilly)
------------------------------------------------------------------

| [winforms-cs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/winforms-cs.asp = JOIN/QUIT

Reply to this message...
 
    
Dan Hurwitz (VIP)
Thank you for the responses, but removing the image prior to replacing
an image did not help. I used the RemoveAt method, but all it did was
move the superposition to the prior image in the list. =20

Below is a very short, complete program which demonstrates the problem
succinctly. The line w/ RemoveAt is commented out, but you can
uncomment it to see the effect.

Marisk suggested using an Invalidate, but that should not necessary,
since the ImageList is being created in the form constructor and it has
not yet been drawn the first time.

Any further suggestions would be very much appreciated.

Thanks.

--d

**********
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ProgrammingWinApps
{
    public class ImageListBug : Form
    {
        ImageList imgList;
        Label lbl;

        public ImageListBug()
        {
            Text =3D "ImageLists - Bug";
    Size =3D new Size(300,300);

            imgList =3D new ImageList();
            Image img;

            // Use an array to add filenames to the
ImageList
            String[] arFiles =3D {
                @"C:\Program Files\Microsoft Visual
Studio .NET\Common7\" +=20
             @"Graphics\bitmaps\assorted\happy.bmp",
                @"C:\Program Files\Microsoft Visual
Studio .NET\Common7\" +=20
            @"Graphics\icons\industry\rocket.ico"
                };

            for (int i =3D 0; i < arFiles.Length; i++)
            {
                img =3D Image.FromFile(arFiles[i]);
                imgList.Images.Add(img);
            }

            // Change the size
            imgList.ImageSize =3D new Size(32, 32);

            // Replace an image
            img =3D Image.FromFile(
                    "C:\\Program Files\\Microsoft
Visual Studio .NET\\Common7\\" +=20
=20
"Graphics\\icons\\industry\\wrench.ico");
// This line only moves the problem back one image
//            imgList.Images.RemoveAt(imgList.Images.Count -
1);
            imgList.Images[imgList.Images.Count - 1] =3D img;

            lbl =3D new Label();
    lbl.Parent =3D this;
    lbl.Text =3D "The quick brown fox...";
    lbl.Location =3D new Point(0,0);
    lbl.Size =3D new Size(lbl.PreferredWidth +
imgList.ImageSize.Width,=20
                imgList.ImageSize.Height + 10);
    lbl.BorderStyle =3D BorderStyle.Fixed3D;
            lbl.ImageList =3D imgList;
            lbl.ImageIndex =3D 1;
            lbl.ImageAlign =3D ContentAlignment.MiddleRight;

        } =20

        static void Main()=20
        {
    Application.Run(new ImageListBug());
        }
    }
}

Reply to this message...
 
    
Stephen Rees

Dan ...

Whats happening is that when you remove th eimage the imgList gets shorter
by 1.
So then rather than just change the image at the new index you actually need
to add another one.

You still get a scrap of it left but invalidate() it should fix that.

NOT a bug ... that little green man again.

Steve.

-----Original Message-----
From: Dan Hurwitz [mailto:Click here to reveal e-mail address]
Sent: 15 June 2002 19:16
To: winforms-cs
Subject: [winforms-cs] RE: ImageList Strange Behavior

Thank you for the responses, but removing the image prior to replacing
an image did not help. I used the RemoveAt method, but all it did was
move the superposition to the prior image in the list.

Below is a very short, complete program which demonstrates the problem
succinctly. The line w/ RemoveAt is commented out, but you can
uncomment it to see the effect.

Marisk suggested using an Invalidate, but that should not necessary,
since the ImageList is being created in the form constructor and it has
not yet been drawn the first time.

Any further suggestions would be very much appreciated.

Thanks.

--d

**********
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ProgrammingWinApps
{
    public class ImageListBug : Form
    {
        ImageList imgList;
        Label lbl;

        public ImageListBug()
        {
            Text = "ImageLists - Bug";
    Size = new Size(300,300);

            imgList = new ImageList();
            Image img;

            // Use an array to add filenames to the
ImageList
            String[] arFiles = {
                @"C:\Program Files\Microsoft Visual
Studio .NET\Common7\" +
             @"Graphics\bitmaps\assorted\happy.bmp",
                @"C:\Program Files\Microsoft Visual
Studio .NET\Common7\" +
            @"Graphics\icons\industry\rocket.ico"
                };

            for (int i = 0; i < arFiles.Length; i++)
            {
                img = Image.FromFile(arFiles[i]);
                imgList.Images.Add(img);
            }

            // Change the size
            imgList.ImageSize = new Size(32, 32);

            // Replace an image
            img = Image.FromFile(
                    "C:\\Program Files\\Microsoft
Visual Studio .NET\\Common7\\" +

"Graphics\\icons\\industry\\wrench.ico");
// This line only moves the problem back one image
//            imgList.Images.RemoveAt(imgList.Images.Count -
1);
            imgList.Images[imgList.Images.Count - 1] = img;

            lbl = new Label();
    lbl.Parent = this;
    lbl.Text = "The quick brown fox...";
    lbl.Location = new Point(0,0);
    lbl.Size = new Size(lbl.PreferredWidth +
imgList.ImageSize.Width,
                imgList.ImageSize.Height + 10);
    lbl.BorderStyle = BorderStyle.Fixed3D;
            lbl.ImageList = imgList;
            lbl.ImageIndex = 1;
            lbl.ImageAlign = ContentAlignment.MiddleRight;

        }

        static void Main()
        {
    Application.Run(new ImageListBug());
        }
    }
}

| [winforms-cs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/winforms-cs.asp = JOIN/QUIT

Reply to this message...
 
    
Stephen Rees
what that also means is that since there is no replace method then if you do
want to change an image in a list then you is going to have to reset the
whole list, or every thing after than index.
Could have done with a replace method eh ?
(write your own funtion to insert an image into it, or replace one ... just
a bit of messing about with arrays ... and reusable !)

Don't tell me that dotNET is going to be riddled with things like this so
people can make a "new" image list ... one they can flog and that has got a
insert/replace method ?

Steve.

-----Original Message-----
From: Dan Hurwitz [mailto:Click here to reveal e-mail address]
Sent: 15 June 2002 19:16
To: winforms-cs
Subject: [winforms-cs] RE: ImageList Strange Behavior

Thank you for the responses, but removing the image prior to replacing
an image did not help. I used the RemoveAt method, but all it did was
move the superposition to the prior image in the list.

Below is a very short, complete program which demonstrates the problem
succinctly. The line w/ RemoveAt is commented out, but you can
uncomment it to see the effect.

Marisk suggested using an Invalidate, but that should not necessary,
since the ImageList is being created in the form constructor and it has
not yet been drawn the first time.

Any further suggestions would be very much appreciated.

Thanks.

--d

**********
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ProgrammingWinApps
{
    public class ImageListBug : Form
    {
        ImageList imgList;
        Label lbl;

        public ImageListBug()
        {
            Text = "ImageLists - Bug";
    Size = new Size(300,300);

            imgList = new ImageList();
            Image img;

            // Use an array to add filenames to the
ImageList
            String[] arFiles = {
                @"C:\Program Files\Microsoft Visual
Studio .NET\Common7\" +
             @"Graphics\bitmaps\assorted\happy.bmp",
                @"C:\Program Files\Microsoft Visual
Studio .NET\Common7\" +
            @"Graphics\icons\industry\rocket.ico"
                };

            for (int i = 0; i < arFiles.Length; i++)
            {
                img = Image.FromFile(arFiles[i]);
                imgList.Images.Add(img);
            }

            // Change the size
            imgList.ImageSize = new Size(32, 32);

            // Replace an image
            img = Image.FromFile(
                    "C:\\Program Files\\Microsoft
Visual Studio .NET\\Common7\\" +

"Graphics\\icons\\industry\\wrench.ico");
// This line only moves the problem back one image
//            imgList.Images.RemoveAt(imgList.Images.Count -
1);
            imgList.Images[imgList.Images.Count - 1] = img;

            lbl = new Label();
    lbl.Parent = this;
    lbl.Text = "The quick brown fox...";
    lbl.Location = new Point(0,0);
    lbl.Size = new Size(lbl.PreferredWidth +
imgList.ImageSize.Width,
                imgList.ImageSize.Height + 10);
    lbl.BorderStyle = BorderStyle.Fixed3D;
            lbl.ImageList = imgList;
            lbl.ImageIndex = 1;
            lbl.ImageAlign = ContentAlignment.MiddleRight;

        }

        static void Main()
        {
    Application.Run(new ImageListBug());
        }
    }
}

| [winforms-cs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/winforms-cs.asp = JOIN/QUIT

Reply to this message...
 
    
Stephen Rees

My mistake ... no need to invalidate.

I had added an image AND set the image at the index !

So just RemoteAT and then Add the new one.
(don't use this line at all :: imgList.Images[imgList.Images.Count - 1] =
img;)

Steve.

-----Original Message-----
From: Dan Hurwitz [mailto:Click here to reveal e-mail address]
Sent: 15 June 2002 19:16
To: winforms-cs
Subject: [winforms-cs] RE: ImageList Strange Behavior

Thank you for the responses, but removing the image prior to replacing
an image did not help. I used the RemoveAt method, but all it did was
move the superposition to the prior image in the list.

Below is a very short, complete program which demonstrates the problem
succinctly. The line w/ RemoveAt is commented out, but you can
uncomment it to see the effect.

Marisk suggested using an Invalidate, but that should not necessary,
since the ImageList is being created in the form constructor and it has
not yet been drawn the first time.

Any further suggestions would be very much appreciated.

Thanks.

--d

**********
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ProgrammingWinApps
{
    public class ImageListBug : Form
    {
        ImageList imgList;
        Label lbl;

        public ImageListBug()
        {
            Text = "ImageLists - Bug";
    Size = new Size(300,300);

            imgList = new ImageList();
            Image img;

            // Use an array to add filenames to the
ImageList
            String[] arFiles = {
                @"C:\Program Files\Microsoft Visual
Studio .NET\Common7\" +
             @"Graphics\bitmaps\assorted\happy.bmp",
                @"C:\Program Files\Microsoft Visual
Studio .NET\Common7\" +
            @"Graphics\icons\industry\rocket.ico"
                };

            for (int i = 0; i < arFiles.Length; i++)
            {
                img = Image.FromFile(arFiles[i]);
                imgList.Images.Add(img);
            }

            // Change the size
            imgList.ImageSize = new Size(32, 32);

            // Replace an image
            img = Image.FromFile(
                    "C:\\Program Files\\Microsoft
Visual Studio .NET\\Common7\\" +

"Graphics\\icons\\industry\\wrench.ico");
// This line only moves the problem back one image
//            imgList.Images.RemoveAt(imgList.Images.Count -
1);
            imgList.Images[imgList.Images.Count - 1] = img;

            lbl = new Label();
    lbl.Parent = this;
    lbl.Text = "The quick brown fox...";
    lbl.Location = new Point(0,0);
    lbl.Size = new Size(lbl.PreferredWidth +
imgList.ImageSize.Width,
                imgList.ImageSize.Height + 10);
    lbl.BorderStyle = BorderStyle.Fixed3D;
            lbl.ImageList = imgList;
            lbl.ImageIndex = 1;
            lbl.ImageAlign = ContentAlignment.MiddleRight;

        }

        static void Main()
        {
    Application.Run(new ImageListBug());
        }
    }
}

| [winforms-cs] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/winforms-cs.asp = JOIN/QUIT

Reply to this message...
 
 
System.Drawing.ContentAlignment
System.Drawing.Point
System.Drawing.Size
System.Reflection.Emit.Label
System.Web.UI.MobileControls.Image
System.Web.UI.MobileControls.Label
System.Web.UI.WebControls.Label
System.Windows.Forms.Application
System.Windows.Forms.BorderStyle
System.Windows.Forms.ImageList
System.Windows.Forms.Label
System.Windows.Forms.NumericUpDown




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com