2012-04-04 199 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

相關問題