是否存在PictureBox控件的图像更改事件?

时间:2022-04-28 05:53:56

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

我如何知道图片框的图像何时发生变化?是否有图像更改事件?

3 个解决方案

#1


9  

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).

首先确保图像是异步加载的。为此,请将PictureBox的WaitOnLoad属性设置为false(这是默认值)。

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.

为PictureBox的LoadCompleted事件创建一个事件处理程序。异步映像加载操作完成,取消或导致异常时会触发此事件。

pictureBox1.LoadCompleted += PictureBox1_LoadCompleted;

private void PictureBox1_LoadCompleted(Object sender, AsyncCompletedEventArgs e) 
{       
    //...       
}

You can find more information about this event on MSDN:

您可以在MSDN上找到有关此活动的更多信息:

http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.loadcompleted.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.loadcompleted.aspx

#2


6  

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

您可以将此类添加到ToolBox和/或代码中,并使用ImageChanged事件来捕获Image是否已更改

#3


4  

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.

如果使用Load()或LoadAsync(),则存在加载事件,但Image属性不存在。这是由您(开发人员)明确设置的,通常是100%的控制权(请参阅下面的注释)。如果你真的想要(虽然没有意义),你可以从PictureBox派生自己的UserControl并覆盖Image属性,并实现自己的事件处理程序。

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.

符号我想如果您使用某些更改图像属性的第三方组件或控件,并且您希望在发生这种情况时实现某种子例程,那么您希望事件订阅的一个事件。在这种情况下,由于您无法控制图像的设置,因此需要ImageChanged事件是一个原因。不幸的是,仍然没有办法绕过这种情况。

#1


9  

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).

首先确保图像是异步加载的。为此,请将PictureBox的WaitOnLoad属性设置为false(这是默认值)。

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.

为PictureBox的LoadCompleted事件创建一个事件处理程序。异步映像加载操作完成,取消或导致异常时会触发此事件。

pictureBox1.LoadCompleted += PictureBox1_LoadCompleted;

private void PictureBox1_LoadCompleted(Object sender, AsyncCompletedEventArgs e) 
{       
    //...       
}

You can find more information about this event on MSDN:

您可以在MSDN上找到有关此活动的更多信息:

http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.loadcompleted.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.loadcompleted.aspx

#2


6  

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

您可以将此类添加到ToolBox和/或代码中,并使用ImageChanged事件来捕获Image是否已更改

#3


4  

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.

如果使用Load()或LoadAsync(),则存在加载事件,但Image属性不存在。这是由您(开发人员)明确设置的,通常是100%的控制权(请参阅下面的注释)。如果你真的想要(虽然没有意义),你可以从PictureBox派生自己的UserControl并覆盖Image属性,并实现自己的事件处理程序。

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.

符号我想如果您使用某些更改图像属性的第三方组件或控件,并且您希望在发生这种情况时实现某种子例程,那么您希望事件订阅的一个事件。在这种情况下,由于您无法控制图像的设置,因此需要ImageChanged事件是一个原因。不幸的是,仍然没有办法绕过这种情况。