@Nedudale , you could try to use Mouse_Leave event to get what you want.
Here is a code example you could refer to.
private void Form1_Load(object sender, EventArgs e)
{
string path = Path.Combine("D:\\", "1.PNG");
pictureBox1.Image = Image.FromFile(path);
}
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
PictureBox pb = new PictureBox();
pb.Name = "pic1";
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Size = new Size(512, 512);
pb.Left = (this.ClientSize.Width - pb.Width) / 2;
pb.Top = (this.ClientSize.Height - pb.Height) / 2;
pb.BringToFront();
string path = Path.Combine("D:\\", "2.jpg");
pb.Image = new Bitmap(path);
this.Controls.Add(pb);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
foreach (Control item in this.Controls)
{
if(item.Name=="pic1")
{
item.Visible = false;
}
}
}
Result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.