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:
Change the assignment so you assign using the ImageLocation
pictureBox10.ImageLocation = @"icon1.png";
Change the check to see if the Image property is equal to your new Image
pictureBox10.Image == Image.FromFile(@"icon.png");
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