Reputation: 340
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.
Upvotes: 0
Views: 1229
Reputation: 340
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
Upvotes: 0
Reputation: 3189
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
Upvotes: 2