Search code examples
c#imagewinformsbitmapimagelist

Properties.Resources.Image not working with ImageList


I am working on a game with dice and when they are clicked, the change color but keep the same number.

I am using ImageLists (as a requirement) and using red and blue die. They are setup using Bitmaps. I'm not really sure how Images versus Bitmaps work, but I saw a suggestion to use Bitmaps so I took it.

 private Bitmap redDie1 = Properties.Resources.die1;
    private Bitmap blueDie1 = Properties.Resources.die1s;
    private Bitmap redDie2 = Properties.Resources.die2;
    private Bitmap blueDie2 = Properties.Resources.die2s;
    private Bitmap redDie3 = Properties.Resources.die3;
    private Bitmap blueDie3 = Properties.Resources.die3s;
    private Bitmap redDie4 = Properties.Resources.die4;
    private Bitmap blueDie4 = Properties.Resources.die4s;
    private Bitmap redDie5 = Properties.Resources.die5;
    private Bitmap blueDie5 = Properties.Resources.die5s;
    private Bitmap redDie6 = Properties.Resources.die6;
    private Bitmap blueDie6 = Properties.Resources.die6s;

I then setup the ImageLists (redDieImages, blueDieImages) I want to use by adding the variables to them. Using the variable testImages I see if redDie1 the variable, and the redDie1 that I added to redDieImages were equal. The variable is false in debug mode.

      private void Form1_Load(object sender, EventArgs e)
    {
        diceLabels[0] = diceLabel1;
        diceLabels[1] = diceLabel2;
        diceLabels[2] = diceLabel3;
        diceLabels[3] = diceLabel4;
        diceLabels[4] = diceLabel5;

        redDieImages.Images.Add(redDie1);
        redDieImages.Images.Add(redDie2);
        redDieImages.Images.Add(redDie3);
        redDieImages.Images.Add(redDie4);
        redDieImages.Images.Add(redDie5);
        redDieImages.Images.Add(redDie6);
        //redDieImages.ImageSize = new Size(dieImageSize, dieImageSize);

        blueDieImages.Images.Add(blueDie1);
        blueDieImages.Images.Add(blueDie2);
        blueDieImages.Images.Add(blueDie3);
        blueDieImages.Images.Add(blueDie4);
        blueDieImages.Images.Add(blueDie5);
        blueDieImages.Images.Add(blueDie6);
        //blueDieImages.ImageSize = new Size(dieImageSize, dieImageSize);

        bool testImages = redDie1 == redDieImages.Images[0];

I am using the imagelists to see which of the labels that will be clicked to change the color of the die contains each picture (6 pictures of dice, sides 1-6). The dicelabelIndex is just from an array of the labels that I am using to store the images. In ContainsImage I am trying to check which index of the redDieImages contains the red image so I can replace it with the blue version of the image. The diceLabel click allows me to change the color of the image using the ContainsImage method.

       private int ContainsImage(int diceLabelsIndex)
    {
        int itemIndex = -1;
        int count = redDieImages.Images.Count;

        for (int i = 0; i < redDieImages.Images.Count; i++)
        {
            if (diceLabels[diceLabelsIndex].Image == redDieImages.Images[i])
            {
                itemIndex = i;
                break;
            }
        }

        return itemIndex;
    }

    private void diceLabel1_Click(object sender, EventArgs e)
    {
        int imageIndex = ContainsImage(0);

        if (diceLabel1.Image == redDieImages.Images[imageIndex])
        {
            diceLabel1.Image = blueDieImages.Images[imageIndex];
        }
        else
        {
            diceLabel1.Image = redDieImages.Images[imageIndex];
        }
    }

Overall I need help in making sure that the images are equal so that way they can be compared in ContainsImage so that way I can switch the images when the user clicks on the label. Yet the images are not equal even though they are both from the same variable.

Thanks for any help


Solution

  • ImageList does not store the Images but modified copies of them. So you don't create just a copy of a reference to the same thing!

    Look at the ImageSize and ColorDepth variables! They are serious: After adding an image to an ImageList a new image with the new size and color depth is created.

    So redDie1 == redDieImages.Images[0] indeed is always false.

    Instead you can use a string as the Key to the images:

    First store it in the Bitmaps' Tag property:

    private Bitmap redDie1 = Properties.Resources.die1;
    redDie1.Tag = "redDie1";
    

    Next use it when you add the images to the ImageList:

    redDieImages.Images.Add(redDie1.Tag.ToString(), redDie1);
    

    Now you can write your test like this:

    bool testImages = redDie1.Tag.ToString() == redDieImages.Images.Keys[0];
    

    Also note the difference between reference and value parameters: Even if the ImageSize and ColorDepth were the same, still a new copy is created and a comparison between two copies is always false. And even if you load two Images variable from the same source, they are not equal..

    In contrast this will be true:

    Bitmap copy1 = redDie1;         // contains a reference to redDie1
    bool test = copy1 == redDie1;   // true
    

    For completeness' sake: the system makes an exception to the rule for strings to allow for more natural comparisons, but not for images.

    Btw: Image is a superclass the can be several things, most notably Bitmap or Icon ..