2012-04-04 201 views
5

已经决定尝试AForge视频和成像的东西,我试图执行this simple demo更新PictureBox时可能导致ArgumentException的原因是什么?

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; 
} 

的问题是,我总是得到一个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属性异常为空。我尝试在try ... catch块中包装图像分配,但这没有帮助。我也检查过,以确保图像在赋值之前不为空。我也检查了非空,但0x0大小的图像。

我做错了什么?任何人都可以提出一个解决方法?

+1

异常中的消息是什么? – 2012-04-04 16:16:40

+1

@DavidNelson很好的问题:“参数无效”。 – 2012-04-04 16:18:43

+0

设置eventArgs.Frame时的高度和宽度是多少? – 2012-04-04 16:22:19

回答

5

我认为问题在于,您不会在事件处理程序中复制传递的位图(帧)的 。

的AForge文件说:

由于视频源可以有多个客户端,每个客户端是为了使通过视频帧的复制(克隆)负责 ,因为视频源 处置自己的原创在通知客户后复制。

所以,如果你直接帧分配给图片框 位图可以由AForge框架而PictureBox 试图绘制位图配置。

+0

这是一个胜利者。目的是马上绘制它,而不是稍后离开它。没有更晚的视频播放。 – 2012-04-04 17:45:27

+0

这样做 - 谢谢。 – 2012-04-04 17:45:38

相关问题