0

I have one image in my picture box using the resource what i want is to change the image when i click the icon.png it should be changed to icon1.png within the picturebox then when i click again the picture box it should be changed to icon.png

private void pictureBox10_Click(object sender, EventArgs e)
    {
        if (pictureBox10.ImageLocation != @"icon1.png")
        {
            var image = Image.FromFile(@"icon1.png");
            pictureBox10.Image = image;

        }
        if (pictureBox10.ImageLocation == @"icon1.png")
        {
            var image = Image.FromFile(@"icon.png");
            pictureBox10.Image = image;
        }

    }

but its not working please help me out of this.

Matthew Walton
  • 9,809
  • 3
  • 27
  • 36
arn48
  • 310
  • 4
  • 12
  • what have you tried? what happens? have you tried out calling `DoEvents()`? – Mafii Mar 17 '16 at 08:20
  • i just use the onlick function to change the image and picurebox10.imagelocation is giving a null value thats why its only work with the first if condition – arn48 Mar 17 '16 at 08:24
  • When you debug the code, what comes up in the ImageLocation property? I'm not 100% sure that setting the Image property will also set the ImageLocation property – Draken Mar 17 '16 at 08:25
  • it works but only for the first if condition where the value of picturebox10.imagelocation is null so it satisfies the first condition so only for first condition image is changing – arn48 Mar 17 '16 at 08:28
  • actually the condition is not working – arn48 Mar 17 '16 at 08:30
  • Can you please paste the code you've used to initialise the PcitureBox and set it's image? – Draken Mar 17 '16 at 08:45
  • Debug your code add watch the value of pictureBox10.ImageLocation as change the image. – Sarvesh Mishra Mar 17 '16 at 09:21

3 Answers3

2

You're getting a null from the image location as it's not set when you're assigning a picture to the Image property. There are a few ways to fix this:

  1. Change the assignment so you assign using the ImageLocation

    pictureBox10.ImageLocation = @"icon1.png";
    
  2. Change the check to see if the Image property is equal to your new Image

    pictureBox10.Image == Image.FromFile(@"icon.png");
    
  3. Set the image location at the same time you set the image property

    pictureBox10.Image == Image.FromFile(@"icon.png");
    pictureBox10.ImageLocation = @"icon.png" ;
    

I feel the second might not come back as equal, you probably want to try the first one or third

Suggested code:

private void pictureBox10_Click(object sender, EventArgs e)
{
    if (pictureBox10.ImageLocation != @"icon1.png")
    {
        pictureBox10.ImageLocation = @"icon1.png"

    }
    if (pictureBox10.ImageLocation == @"icon1.png")
    {
        pictureBox10.ImageLocation = @"icon.png";
    }

}

Or:

private void pictureBox10_Click(object sender, EventArgs e)
{
    if (pictureBox10.ImageLocation != @"icon1.png")
    {
        var image = Image.FromFile(@"icon1.png");
        pictureBox10.Image = image;
        pictureBox10.ImageLocation = @"icon1.png";

    }
    if (pictureBox10.ImageLocation == @"icon1.png")
    {
        var image = Image.FromFile(@"icon.png");
        pictureBox10.Image = image;
        pictureBox10.ImageLocation = @"icon.png";
    }

}

You would also need to update your intial property setting to set the ImageLocation and not the Image property or to set the ImageLocation at the same time you set the Image file

EDIT

Off the top of my head, to set the property initially, you can do this (Source):

protected override void OnLoad(EventArgs e){
    pictureBox10.ImageLocation = @"icon.png";
}

Though I can't remember if the PictureBox would have been created then, if not then use the onShown event instead (Source)

EDIT 2

Here is another way to create the event and set the property, first follow the steps here to add the event onShown to the form. You need to click on the form itself and not the controls inside the form to find the event.

Once done, inside the event add the following code:

pictureBox10.ImageLocation = @"icon.png";

That should help to resolve your issue

Community
  • 1
  • 1
Draken
  • 3,134
  • 13
  • 34
  • 54
  • you know the property is working fine but the condition is not right cause ipicturebox10.location is returning a null value – arn48 Mar 17 '16 at 08:33
  • 1
    It's returning null as it's not been set, when you set the property Image it does not set the property ImageLocation at the same time! – Draken Mar 17 '16 at 08:34
  • What's not working now @arn48 is the ImageLocation property set or not? – Draken Mar 17 '16 at 08:44
  • when i copy the code and paste it doing nothing so whats the problem? – arn48 Mar 17 '16 at 08:54
  • That's because it's probably not been set when the PictureBox has first been created. Did you initialise the PictureBox or have you used visual studio to auto generate the code? If the first, you need to set the ImageLocation at the first point you set the Image, if the second then during the form building, (maybe in your constructor), you need to manually set the property of ImageLocation – Draken Mar 17 '16 at 08:57
  • Ok but i dont know how to set it manually so can u just help me? – arn48 Mar 17 '16 at 09:10
  • ya have added if (pictureBox10.ImageLocation != "icon1.png") { pictureBox10.ImageLocation = @"icon.png"; var image = Image.FromFile(@"icon1.png"); pictureBox10.Image = image; //pictureBox10.Refresh(); } but why its not working? why not changing the image? – arn48 Mar 17 '16 at 09:37
  • @arn48 What is pictureBox10.ImageLocation set to when you debug it? You're also missing an @ in front of the "icon1.png" in your check – Draken Mar 17 '16 at 09:43
  • pictureBox10.ImageLocation = @"icon.png"; – arn48 Mar 17 '16 at 09:44
  • Does it enter the if structure or does it just skip over it? – Draken Mar 17 '16 at 09:46
  • @arn48 I'm sorry but I do not understand your question, what do you mean by can I have the code? – Draken Mar 17 '16 at 10:02
  • thanks a lot but can u give me the proper working code cause everything fails for me – arn48 Mar 17 '16 at 10:06
  • Why is it failing? What debugging have you done? Have you added the onShown event as I suggested? Does the code hit that when you start the form? What happens at the if statement? I can't just give you code as I'm not at a computer that has Visual Studio and you also need to learn how to do this. Debugging is an essential skill for any developer and if you are having difficulty conveying your issues, you're going to have more problems when you get to more complicated things – Draken Mar 17 '16 at 10:11
0

Try to directly reference the picture into the picture box:

pictureBox10.Image = Image.FromFile(@"Images\a.bmp");

Source

Community
  • 1
  • 1
Mafii
  • 7,227
  • 1
  • 35
  • 55
0

Thanks every one thanks a lot but there was some problem but have solved this issue so m writing the actual code...

public Form1()
    {
        InitializeComponent();
        pictureBox10.ImageLocation = @"icon.png";
    }
    private void pictureBox10_Click(object sender, EventArgs e)
    {

        if (pictureBox10.ImageLocation == @"icon1.png")
        {
            pictureBox10.ImageLocation = @"icon.png";

        }
        else
        {
            pictureBox10.ImageLocation = @"icon1.png";
        }



    }

first you have to initialize the image location then use two if condition i think that was the main problem use if else anyway thaks every one thaks a lot special thaks to @Draken

arn48
  • 310
  • 4
  • 12