I have code that creates a PictureBox and I want it's background Image to change when I click it.

My code:

    private void CreatImage()
    {
         Random LocationPicker = new Random();

         for (int i = 0; i < 50; i++)
         {
            int X = LocationPicker.Next(0, 842);
            int Y = LocationPicker.Next(245, 433);

            PictureBox Image = new PictureBox();
            Image.Name = "Image";             Image.BackgroundImage = Properties.Resources.Image24;
            Image.BackColor = Color.Transparent;
            Image.BackgroundImageLayout = ImageLayout.Stretch;
            Image.Size = new Size(30, 30);
            Image.Location = new Point(X, Y);
            UpdateImage();
            this.Controls.Add(Image);
        }
    }

    private void UpdateImage()
    {

        //How do I change the Image I created above to have a diffrent BackgroundImage?

    }

UpdateImage() knows nothing about an Image so how could it update it?
You got to have more something like: private void UpdateImage(PictureBox aPict)

PictureBoxes have BackgroundImages ... how the heck did I not know this.

For the code, let me provide you with a few warnings I have learned over time of messing with images. First of all, if you can load the image in (if calling a seperate function) instead of passing it as a parameter do so. Next, make sure to always watch you don't leave old images laying around.

Now in your case you are using a Resource. Now I am pretty sure on this. I have been using a few images from the resources myself, and seen this happen. What I have come to learn is that everytime you assign a resource to a variable/property it creates a new instance of the image, and not just an referenced to an existing one in memory. By this I mean

Image image = Image.FromFile("Filename.jpg");

PictureBox1.Image = image;
PictureBox2.Image = image;

That code right there shows that one image is loaded into memory once, and both PictureBox images, as well as the Image variable itself are all connected. So should you change one it will effect them all. However, as I mentioned, when pulling for a resource, you are create a new variable each time. So be careful when doing this, if it's a large enough image and you aren't disposing of your images, you could have a nice memory leak grow.

If you want to change the image here's a way (we'll pass a parameter, since it's a property)

private void UpdateImage (Image MyNewImage)
{
    try
    {
        if (Image.BackgroundImage != null)
        {
            Image.BackgroundImage.Dispose();
        }

        Image.BackgroundImage = (Image) MyNewImage.Clone();
    }
    catch
    { }
    finally
    {
        if(MyNewImage != null)
        {
            MyNewImage.Dispose();
        }
    }       
}

I know that's probably a lot more then you were looking for, I've just been working with images on some projects for the last few years, and have dealt with the how they handle memory and the annoyance it can cause all to much.

commented: Nice comment +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.