Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an event for an image change for a PictureBox Control?

How do I know when the image of the picturebox change?
Is there an event for an image change?

like image 209
sari k Avatar asked Sep 26 '11 06:09

sari k


4 Answers

The Paint event is working:

private void pictureBox_Paint(object sender, PaintEventArgs e)
{
    // image has changed
}
like image 123
Jonathan Avatar answered Nov 17 '22 04:11

Jonathan


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.

like image 40
David Anderson Avatar answered Nov 17 '22 06:11

David Anderson


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

like image 10
Rosmarine Popcorn Avatar answered Nov 17 '22 06:11

Rosmarine Popcorn


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

like image 9
Christophe Geers Avatar answered Nov 17 '22 06:11

Christophe Geers