PictureBox 控件是否有图像更改事件?

发布于 2024-12-06 08:42:57 字数 40 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伴随着你 2024-12-13 08:42:57

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

pictureBox1.WaitOnLoad = false;

然后异步加载图像:

pictureBox1.LoadAsync("neutrinos.gif");

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

pictureBox1.LoadCompleted += PictureBox1_LoadCompleted;

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

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

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

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

单身情人 2024-12-13 08:42:57
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());
            }
        }
    }
}

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

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

疯到世界奔溃 2024-12-13 08:42:57

如果您使用 Load()LoadAsync(),则会产生加载事件,但对于 Image 属性则不会。这是由您(开发人员)明确设置的,一般来说,您可以 100% 控制(参见下面的符号)。如果您确实想要(不过实际上没有意义),您可以从 PictureBox 派生您自己的 UserControl 并覆盖 Image > 属性,并实现您自己的事件处理程序。

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

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.

执笔绘流年 2024-12-13 08:42:57

Paint 事件正在运行:

private void pictureBox_Paint(object sender, PaintEventArgs e)
{
    // image has changed
}

The Paint event is working:

private void pictureBox_Paint(object sender, PaintEventArgs e)
{
    // image has changed
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文