How do I know when the image of the picturebox change?
Is there an event for an image change?
The Paint
event is working:
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
// image has changed
}
There are load events if you use Load()
or LoadAsync()
, but not for the Image
property. This is explicitly set by you (the developer) and is generally speaking in 100% of your control (see notation below). If you really wanted to though (there isn't really a point, though), you can derive your own UserControl
from PictureBox
and override the Image
property, and implement your own event handler.
Notation I suppose one event you would want an event to subscribe to is if you are using some third-party component or control that changes the image property, and you want to implement some sort of sub routine when this happens. In this event it would be a reason to need a ImageChanged event since you don't have control over when the image is set. Unfortunately there still isn't a way to circumvent this scenario.
using System;
using System.Windows.Forms;
using System.Drawing;
namespace CustomPX
{
public class CustomPictureBox : PictureBox
{
public event EventHandler ImageChanged;
public Image Image
{
get
{
return base.Image;
}
set
{
base.Image = value;
if (this.ImageChanged != null)
this.ImageChanged(this, new EventArgs());
}
}
}
}
You can add this Class into ToolBox and/or from code and use ImageChanged event to catch if Image is Changed
First make sure that the images are loaded asynchronously. To do this set the PictureBox's WaitOnLoad property to false (which is the default value).
pictureBox1.WaitOnLoad = false;
Then load the image asynchronously:
pictureBox1.LoadAsync("neutrinos.gif");
Create an event handler for the PictureBox's LoadCompleted event. This event is triggered when the asynchronous image-load operation is completed, canceled, or caused an exception.
pictureBox1.LoadCompleted += PictureBox1_LoadCompleted;
private void PictureBox1_LoadCompleted(Object sender, AsyncCompletedEventArgs e)
{
//...
}
You can find more information about this event on MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.loadcompleted.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With