更新PictureBox时可能导致ArgumentException的原因是什么?

Tom*_*ght 5 c# picturebox argumentexception aforge

我决定尝试AForge的视频和图像处理,我尝试实现这个简单的演示:

private void Main_Load(object sender, EventArgs e)
{
        // enumerate video devices
        FilterInfoCollection videoDevices = new FilterInfoCollection(
                        FilterCategory.VideoInputDevice);
        // create video source
        VideoCaptureDevice videoSource = new VideoCaptureDevice(
                        videoDevices[0].MonikerString);
        // set NewFrame event handler
        videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
        // start the video source
        videoSource.Start();
}

private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
        this.pictureBox1.Image = eventArgs.Frame;
}
Run Code Online (Sandbox Code Playgroud)

问题是我总是得到一个ArgumentException,但并不总是马上发生.它弹出Application.Run(new Main());,但堆栈跟踪的顶部看起来像这样:

  • at System.Drawing.Image.get_Width() at System.Drawing.Image.get_Size()
  • at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
  • at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)

不确定这是否相关,但ParamName异常的属性为null.我试过在try ... catch块中包装图像赋值,但这没有帮助.我还检查过以确保图像在分配之前不为空.我还检查了非null,但是0x0大小的图像.

我做错了什么?任何人都可以建议解决方法?

Han*_*ans 5

我认为问题是你没有在事件处理程序中复制传递的位图(帧).

AForge文件说:

由于视频源可能具有多个客户端,因此每个客户端负责对传递的视频帧进行复制(克隆),因为视频源在通知客户端后处理其自己的原始副本.

因此,如果您直接将帧分配给图片框,则在PictureBox 尝试绘制位图时,AForge框架可以处理位图.