Search code examples
c#picturebox

How to change image in picturebox C#


I need to change the image by clicking on pictureBox, but when I click it again I can't return former picture

Here is my code:

private void PictureBox_Click(object sender, EventArgs e)
{
    if (pictureBox.Image == Properties.Resources.openeye)
        pictureBox.Image = Properties.Resources.closeeye;
    else
        pictureBox.Image = Properties.Resources.openeye;
}

How can I fix it?


Solution

  • Here's an example that demonstrates it with two images. One of an "x" another of an "o".

    As you can see, the form has two instance variables x and o to store each of the Image objects. There is another flag field called isX which the ClickHandle event handler uses to check which image is currently displayed and switch to the other image, the event handler then toggles the isX field so the next click responds properly.

    public void Main(string[] args)
    {
        var f1 = new Form1(); // form instance that holds the PictureBox
    
        Task.Run(() => Application.Run(f1)); //I'm running this from LINQPad, but this would also work in a console application.
    }
    
    public class Form1 : Form // Derives from the Form class
    {
        private bool isX; // private instance variable to indicate which image is diplayed
        private Image x; // private instance variable storing the x image
        private Image o; // private instance variable storing the o image
    
        // the picture box this form uses
        private PictureBox p;
    
        public Form1()
        {
            // load the images from wherever they are stored.
            // I do this at construction time to avoid doing disk IO when clicking
            x = Image.FromFile(@"C:\image\path\x.png");
            o = Image.FromFile(@"C:\image\path\o.png");
    
            // Initialize the picture box
            p = new PictureBox {
                Name = "p1",
                Size = new Size(100,100),
                Location = new Point(100,100),
                Image = o //Initialize with the o image
            };
    
            // register the click event handler
            p.Click += this.ClickHandle;
    
            // set the flag to false, since the o image is what we start with
            this.isX = false;
    
            // add PictureBox p to the form
            this.Controls.Add(p);
        }
    
        // handles the click action, registered to the PictureBox.Click event
        private void ClickHandle(object sender, EventArgs e)
        {
            // use the flag to check which image is shown, and display the other image
            if(this.isX) // this might work with your image == check, I didn't test it
            {
                p.Image = this.o;
            }
            else
            {
                p.Image = this.x;
            }
            // set the flag to the opposite of whatever the flag currently is
            this.isX = ! isX;
        }
    }
    

    o image

    x image