EDN Admin
Well-known member
<pre class="prettyprint lang-vb I took these codes from http://www.codeproject.com/Articles/285964/WPF-Webcam-Control http://www.codeproject.com/Articles/285964/WPF-Webcam-Control and encountered these errors. Imports Microsoft.Expression.Encoder.Devices
Imports WebcamControl
Imports System.IO
Imports Microsoft.Expression.Encoder.Live
Imports Microsoft.Expression.Encoder
Imports System.Drawing
Imports System.Drawing.Imaging
Class MainWindow
Private webCamCtrl As New Webcam
Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Public Class RoutedEventArgs Inherits EventArgs
FindDevices()
Dim vidPath As String = "C:UsersLowyhDesktopRecorded"
Dim imgPath As String = "C:UsersLowyhDesktopsnapshot"
If Directory.Exists(vidPath) = False Then
Directory.CreateDirectory(vidPath)
End If
If Directory.Exists(imgPath) = False Then
Directory.CreateDirectory(imgPath)
End If
Set some properties of the Webcam Control.
webCamCtrl.VideoDirectory = vidPath
webCamCtrl.VidFormat = VideoFormat.wmv
webCamCtrl.ImageDirectory = imgPath
webCamCtrl.PictureFormat = ImageFormat.Jpeg
Set the Webcam control as the ContentControls content.
ContentControl1.Content = webCamCtrl
VidDvcsComboBox.SelectedIndex = 0
AudDvcsComboBox.SelectedIndex = 0
End Sub
Find availabe a/v devices.
Private Sub FindDevices()
Dim vidDevices = EncoderDevices.FindDevices(EncoderDeviceType.Video)
Dim audDevices = EncoderDevices.FindDevices(EncoderDeviceType.Audio)
For Each dvc In vidDevices
VidDvcsComboBox.Items.Add(dvc.Name)
Next
For Each dvc In audDevices
AudDvcsComboBox.Items.Add(dvc.Name)
Next
End Sub
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles StartButton.Click
Display webcam images on control.
Try
webCamCtrl.StartCapture()
Catch ex As Microsoft.Expression.Encoder.SystemErrorException
MessageBox.Show("Device is in use by another application")
End Try
End Sub
Private Sub EndButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles EndButton.Click
Stop the display of webcam video.
webCamCtrl.Stopcapture()
End Sub
Private Sub RecordButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RecordButton.Click
Start recording of webcam video to harddisk.
webCamCtrl.StartRecording()
End Sub
Private Sub StopRecordButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles StopRecordButton.Click
Stop recording of webcam video to harddisk.
webCamCtrl.StopRecording()
End Sub
Private Sub VidDvcsComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles VidDvcsComboBox.SelectionChanged
Set which video device to use.
webCamCtrl.VideoDevice = VidDvcsComboBox.SelectedValue
End Sub
Private Sub AudDvcsComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles AudDvcsComboBox.SelectionChanged
Set which audio device to use.
webCamCtrl.AudioDevice = AudDvcsComboBox.SelectedValue
End Sub
Private Sub SnapshotButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles SnapshotButton.Click
webCamCtrl.TakeSnapshot()
End Sub
Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
Dim bndg_1 As New Binding("SelectedValue")
bndg_1.Source = VidDvcsComboBox
webCamCtrl.SetBinding(Webcam.VideoDeviceProperty, bndg_1)
Dim bndg_2 As New Binding("SelectedValue")
bndg_2.Source = AudDvcsComboBox
webCamCtrl.SetBinding(Webcam.AudioDeviceProperty, bndg_2)
End Sub
Private Sub Webcam_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Set dimensions of WinForm Panel to current dimensions
of the UserControl.
Dim panelWidth As Integer = CInt(Me.ActualWidth)
Dim panelHeight As Integer = CInt(Me.ActualHeight)
WebcamPanel.Width = panelWidth
WebcamPanel.Height = panelHeight
End Sub
Public Property VideoDevice() As String
Get
Return CType(GetValue(VideoDeviceProperty), String)
End Get
Set(ByVal value As String)
SetValue(VideoDeviceProperty, value)
End Set
End Property
Public Shared VideoDeviceProperty As DependencyProperty = DependencyProperty As DependencyProperty = DependencyProperty.Register("VideoDevice", GetType(String), GetType(Webcam),
New FrameworkPropertyMetada(New PropertyChangedCallback( AddressOf VidDeviceChange)))
Private Shared Sub VidDeviceChange(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim deviceName As String = CType(e.NewValue, String)
Dim eDev = EncoderDevices.FindDevices(EncoderDeviceType.Video).Where(Function(dv) dv.Name = deviceName)
If (eDev.Count > 0) Then
CType(source, Webcam_Control.MainWindow
).vidDevice = eDev.First
Try
CType(source, Webcam_Control.MainWindow).Display()
Catch ex As Microsoft.Expression.Encoder.SystemErrorException
Exit Sub
End Try
End If
End Sub
Public Sub StartCapture()
If (canCapture = Flase) Then
cancapture = True
Try
Display()
Catch ex As Microsoft.Expression.Encoder.SystemErrorException
canCapture = False
Throw New Microsoft.Expression.Encoder.SystemErrorException
End Try
Else
Exit Sub
End If
End Sub
Private Sub Display()
If (canCapture = True) Then
If (vidDevice IsNot Nothing) Then
StopRecording()
Dispose()
job = New LiveJob
deviceSource = job.AddDeviceSource(vidDevice, audDevice)
deviceSource.PreviewWindow = New PreviewWindow(New HandleRef(WebcamPanel, WebcamPanel.Handle))
job.ActivateSource(deviceSource)
isCapturing = True
End If
End If
End Sub
Public Sub StartRecording()
If (vidDirectory <> String.Empty AndAlso job IsNot Nothing) Then
If (Directory.Exists(vidDirectory) = False) Then
Throw New DirectoryNotFoundException("The specified directory does not exist")
Exit Sub
End If
If iscapturing is true then it means the control is capturing images
from the webcam.
If (isCapturing = True) Then
StopRecording()
job.PublishFormats.Clear()
Dim timeStamp As String = DateTime.Now.ToString
timeStamp = timeStamp.Replace("/", "-")
timeStamp = timeStamp.Replace(":", ".")
Dim filePath As String = vidDirectory & "WebcamVid" & timeStamp & "." & _vidFormat.ToString
DimfileArchFormat As New FileArchivePublishFormat(filePath)
job.PublishFormats.Add(fileArchFormat)
job.StartEncoding()
_isRecording = True
End If
End If
End Sub
Public Enum VideoFormat
wmv
mp4
flv
End Enum
<summary>
Take snapshot of a webcam image.
</summary>
Public Sub TakeSnapshot()
If (imgDirectory <> String.Empty AndAlso job IsNot Nothing) Then
If (Directory.Exists(imgDirectory) = False) Then
Throw New DirectoryNotFoundException("The specified directory does not exist")
Exit Sub
End If
If isCapturing is true then it means the control is capturing video
from the webcam.
If (isCapturing = True) Then
Dim panelWidth As Integer = CInt(Me.ActualWidth)
Dim panelHeight As Integer = CInt(Me.ActualHeight)
Dim timeStamp As String = DateTime.Now.ToString
timeStamp = timeStamp.Replace("/", "-")
timeStamp = timeStamp.Replace(":", ".")
Dim filePath As String = imgDirectory & "Snapshot" & timeStamp & "." & imgFormat.ToString
Dim pnlPnt As Point = WebcamPanel.PointToScreen(New Point(WebcamPanel.ClientRectangle.X, WebcamPanel.ClientRectangle.Y))
Using bmp As New Bitmap(panelWidth, panelHeight)
Using gcs As Graphics = Graphics.FromImage(bmp)
gcs.CopyFromScreen(pnlPnt, Point.Empty, New Size(panelWidth, panelHeight))
End Using
bmp.Save(filePath, imgformat)
End Using
End If
End If
End Sub
End Class
[/code]
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/142954 <br/>
View the full article
Imports WebcamControl
Imports System.IO
Imports Microsoft.Expression.Encoder.Live
Imports Microsoft.Expression.Encoder
Imports System.Drawing
Imports System.Drawing.Imaging
Class MainWindow
Private webCamCtrl As New Webcam
Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Public Class RoutedEventArgs Inherits EventArgs
FindDevices()
Dim vidPath As String = "C:UsersLowyhDesktopRecorded"
Dim imgPath As String = "C:UsersLowyhDesktopsnapshot"
If Directory.Exists(vidPath) = False Then
Directory.CreateDirectory(vidPath)
End If
If Directory.Exists(imgPath) = False Then
Directory.CreateDirectory(imgPath)
End If
Set some properties of the Webcam Control.
webCamCtrl.VideoDirectory = vidPath
webCamCtrl.VidFormat = VideoFormat.wmv
webCamCtrl.ImageDirectory = imgPath
webCamCtrl.PictureFormat = ImageFormat.Jpeg
Set the Webcam control as the ContentControls content.
ContentControl1.Content = webCamCtrl
VidDvcsComboBox.SelectedIndex = 0
AudDvcsComboBox.SelectedIndex = 0
End Sub
Find availabe a/v devices.
Private Sub FindDevices()
Dim vidDevices = EncoderDevices.FindDevices(EncoderDeviceType.Video)
Dim audDevices = EncoderDevices.FindDevices(EncoderDeviceType.Audio)
For Each dvc In vidDevices
VidDvcsComboBox.Items.Add(dvc.Name)
Next
For Each dvc In audDevices
AudDvcsComboBox.Items.Add(dvc.Name)
Next
End Sub
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles StartButton.Click
Display webcam images on control.
Try
webCamCtrl.StartCapture()
Catch ex As Microsoft.Expression.Encoder.SystemErrorException
MessageBox.Show("Device is in use by another application")
End Try
End Sub
Private Sub EndButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles EndButton.Click
Stop the display of webcam video.
webCamCtrl.Stopcapture()
End Sub
Private Sub RecordButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RecordButton.Click
Start recording of webcam video to harddisk.
webCamCtrl.StartRecording()
End Sub
Private Sub StopRecordButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles StopRecordButton.Click
Stop recording of webcam video to harddisk.
webCamCtrl.StopRecording()
End Sub
Private Sub VidDvcsComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles VidDvcsComboBox.SelectionChanged
Set which video device to use.
webCamCtrl.VideoDevice = VidDvcsComboBox.SelectedValue
End Sub
Private Sub AudDvcsComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles AudDvcsComboBox.SelectionChanged
Set which audio device to use.
webCamCtrl.AudioDevice = AudDvcsComboBox.SelectedValue
End Sub
Private Sub SnapshotButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles SnapshotButton.Click
webCamCtrl.TakeSnapshot()
End Sub
Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
Dim bndg_1 As New Binding("SelectedValue")
bndg_1.Source = VidDvcsComboBox
webCamCtrl.SetBinding(Webcam.VideoDeviceProperty, bndg_1)
Dim bndg_2 As New Binding("SelectedValue")
bndg_2.Source = AudDvcsComboBox
webCamCtrl.SetBinding(Webcam.AudioDeviceProperty, bndg_2)
End Sub
Private Sub Webcam_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Set dimensions of WinForm Panel to current dimensions
of the UserControl.
Dim panelWidth As Integer = CInt(Me.ActualWidth)
Dim panelHeight As Integer = CInt(Me.ActualHeight)
WebcamPanel.Width = panelWidth
WebcamPanel.Height = panelHeight
End Sub
Public Property VideoDevice() As String
Get
Return CType(GetValue(VideoDeviceProperty), String)
End Get
Set(ByVal value As String)
SetValue(VideoDeviceProperty, value)
End Set
End Property
Public Shared VideoDeviceProperty As DependencyProperty = DependencyProperty As DependencyProperty = DependencyProperty.Register("VideoDevice", GetType(String), GetType(Webcam),
New FrameworkPropertyMetada(New PropertyChangedCallback( AddressOf VidDeviceChange)))
Private Shared Sub VidDeviceChange(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim deviceName As String = CType(e.NewValue, String)
Dim eDev = EncoderDevices.FindDevices(EncoderDeviceType.Video).Where(Function(dv) dv.Name = deviceName)
If (eDev.Count > 0) Then
CType(source, Webcam_Control.MainWindow
).vidDevice = eDev.First
Try
CType(source, Webcam_Control.MainWindow).Display()
Catch ex As Microsoft.Expression.Encoder.SystemErrorException
Exit Sub
End Try
End If
End Sub
Public Sub StartCapture()
If (canCapture = Flase) Then
cancapture = True
Try
Display()
Catch ex As Microsoft.Expression.Encoder.SystemErrorException
canCapture = False
Throw New Microsoft.Expression.Encoder.SystemErrorException
End Try
Else
Exit Sub
End If
End Sub
Private Sub Display()
If (canCapture = True) Then
If (vidDevice IsNot Nothing) Then
StopRecording()
Dispose()
job = New LiveJob
deviceSource = job.AddDeviceSource(vidDevice, audDevice)
deviceSource.PreviewWindow = New PreviewWindow(New HandleRef(WebcamPanel, WebcamPanel.Handle))
job.ActivateSource(deviceSource)
isCapturing = True
End If
End If
End Sub
Public Sub StartRecording()
If (vidDirectory <> String.Empty AndAlso job IsNot Nothing) Then
If (Directory.Exists(vidDirectory) = False) Then
Throw New DirectoryNotFoundException("The specified directory does not exist")
Exit Sub
End If
If iscapturing is true then it means the control is capturing images
from the webcam.
If (isCapturing = True) Then
StopRecording()
job.PublishFormats.Clear()
Dim timeStamp As String = DateTime.Now.ToString
timeStamp = timeStamp.Replace("/", "-")
timeStamp = timeStamp.Replace(":", ".")
Dim filePath As String = vidDirectory & "WebcamVid" & timeStamp & "." & _vidFormat.ToString
DimfileArchFormat As New FileArchivePublishFormat(filePath)
job.PublishFormats.Add(fileArchFormat)
job.StartEncoding()
_isRecording = True
End If
End If
End Sub
Public Enum VideoFormat
wmv
mp4
flv
End Enum
<summary>
Take snapshot of a webcam image.
</summary>
Public Sub TakeSnapshot()
If (imgDirectory <> String.Empty AndAlso job IsNot Nothing) Then
If (Directory.Exists(imgDirectory) = False) Then
Throw New DirectoryNotFoundException("The specified directory does not exist")
Exit Sub
End If
If isCapturing is true then it means the control is capturing video
from the webcam.
If (isCapturing = True) Then
Dim panelWidth As Integer = CInt(Me.ActualWidth)
Dim panelHeight As Integer = CInt(Me.ActualHeight)
Dim timeStamp As String = DateTime.Now.ToString
timeStamp = timeStamp.Replace("/", "-")
timeStamp = timeStamp.Replace(":", ".")
Dim filePath As String = imgDirectory & "Snapshot" & timeStamp & "." & imgFormat.ToString
Dim pnlPnt As Point = WebcamPanel.PointToScreen(New Point(WebcamPanel.ClientRectangle.X, WebcamPanel.ClientRectangle.Y))
Using bmp As New Bitmap(panelWidth, panelHeight)
Using gcs As Graphics = Graphics.FromImage(bmp)
gcs.CopyFromScreen(pnlPnt, Point.Empty, New Size(panelWidth, panelHeight))
End Using
bmp.Save(filePath, imgformat)
End Using
End If
End If
End Sub
End Class
[/code]
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/142954 <br/>
View the full article