0

使用我在 http://www.codeproject.com/Articles/285964/WPF-Webcam-Control 處找到的示例來創建一些我需要的攝像頭功能。我主要對快照感興趣,我可以讓他們在上面的例子中工作,沒有任何問題。我的主要問題是,我希望能夠從網絡攝像頭拍攝快照,而不必事先向用戶顯示「預覽窗口」。圖像只是自動保存,沒有任何顯示或任何東西給用戶。下面是我(在vb.net,但我不介意在C#中的答案):使用表達式編碼器的攝像頭快照

Public Shared Function TakeSnapshotReturnBytes(panelHeight As Integer, panelWidth As Integer) As Byte() 
    Dim b() As Byte = Nothing 
    Dim vidDevCol As IEnumerable(Of EncoderDevice) = EncoderDevices.FindDevices(EncoderDeviceType.Video) 
    If vidDevCol IsNot Nothing AndAlso vidDevCol.Count > 0 AndAlso vidDevCol(0) IsNot Nothing Then 
     Dim tmpJob As LiveJob = Nothing 
     Dim lvDevSrc As LiveDeviceSource = Nothing 
     Try 
      tmpJob = New LiveJob 
      Using tmpPanel As New System.Windows.Forms.Panel 
       tmpPanel.Height = panelHeight 
       tmpPanel.Width = panelWidth 

       lvDevSrc = tmpJob.AddDeviceSource(vidDevCol(0), Nothing) 
       lvDevSrc.PreviewWindow = New PreviewWindow(New HandleRef(tmpPanel, tmpPanel.Handle)) 
       tmpJob.ActivateSource(lvDevSrc) 

       Using MS As New IO.MemoryStream() 
        Using bmp As New Bitmap(panelWidth, panelHeight) 
         tmpPanel.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height)) 

         bmp.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg) 
         MS.Position = 0 
         Using br As New IO.BinaryReader(MS) 
          b = br.ReadBytes(CInt(MS.Length)) 
         End Using 
        End Using 
      End Using 
      End Using 
     Finally 
      If lvDevSrc IsNot Nothing Then 
       tmpJob.RemoveDeviceSource(lvDevSrc) 
       lvDevSrc.Dispose() 
      End If 
      If tmpJob IsNot Nothing Then 
       tmpJob.Dispose() 
      End If 
     End Try 
    End If 
    Return b 
End Function 

我得到的是一個灰色的窗口,雖然回來了。我猜我不應該使用'PreviewWindow'對象,但我找不到任何替代品。任何人都有這樣做的運氣?

+0

你有沒有想過這個?我遇到了同樣的問題。 – Herrozerro

+0

不,不好意思。最後使用leadtools多媒體(http://www.leadtools.com/sdk/multimedia.htm)獲取快照。雖然不是免費的。 – Phil

回答

0

更改您這樣的代碼:

tmpJob.ActivateSource(lvDevSrc) 

// This delay let your camera to initialize and ready to capture image. 
// Actualy we should find another safer :) way to do this but just to check if it works! 
System.Threading.Thread.Sleep(5000) 

Using MS As New IO.MemoryStream() 

,並檢查是否該爲你的作品。我猜想你捕捉快照時你的攝像頭沒有初始化。

+0

不幸的是,沒有做到這一點。雖然是個好主意,但我仍然得到一個空白輸出 – Phil