一直在使用我在 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”对象,但我找不到任何替代品。其他人有任何运气这样做吗?