Skip to content

Instantly share code, notes, and snippets.

Created March 19, 2015 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/94bc7aed3cabb31582f9 to your computer and use it in GitHub Desktop.
Save anonymous/94bc7aed3cabb31582f9 to your computer and use it in GitHub Desktop.
Imports Microsoft.Expression.Encoder.Devices
Imports Microsoft.Expression.Encoder.Live
Imports Microsoft.Expression.Encoder
Imports System.Runtime.InteropServices
Public Class Form1
Private _job As LiveJob
Private _deviceSource As LiveDeviceSource
Private _bStartedRecording As Boolean = False
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Text += " - ver. " + Application.ProductVersion
lstVideoDevices.ClearSelected()
For Each edv As EncoderDevice In EncoderDevices.FindDevices(EncoderDeviceType.Video)
lstVideoDevices.Items.Add(edv.Name)
Next
lblVideoDeviceSelectedForPreview.Text = ""
lstAudioDevices.ClearSelected()
For Each eda As EncoderDevice In EncoderDevices.FindDevices(EncoderDeviceType.Audio)
lstAudioDevices.Items.Add(eda.Name)
Next
lblAudioDeviceSelectedForPreview.Text = ""
End Sub
Private Sub btnPreview_Click(sender As System.Object, e As System.EventArgs) Handles btnPreview.Click
Dim video As EncoderDevice = Nothing
Dim audio As EncoderDevice = Nothing
GetSelectedVideoAndAudioDevices(video, audio)
StopJob()
If video Is Nothing Then
Return
End If
' Starts new job for preview window
_job = New LiveJob()
' Checks for a/v devices
If video IsNot Nothing AndAlso audio IsNot Nothing Then
' Create a new device source. We use the first audio and video devices on the system
_deviceSource = _job.AddDeviceSource(video, audio)
_deviceSource.PickBestVideoFormat(New Size(640, 480), 15)
' Get the properties of the device video
Dim sp As SourceProperties = _deviceSource.SourcePropertiesSnapshot()
' Resize the preview panel to match the video device resolution set
panelVideoPreview.Size = New Size(sp.Size.Width, sp.Size.Height)
' Setup the output video resolution file as the preview
'https://msdn.microsoft.com/en-us/magazine/ff714558.aspx
'C:\Program Files (x86)\Microsoft Expression\Encoder 4\SDK\Doc\Microsoft.Expression.Encoder.chm
'_job.OutputFormat.VideoProfile = New Profiles.VariableQualityBitrate(95)
_job.OutputFormat.VideoProfile.Size = New Size(sp.Size.Width, sp.Size.Height)
_job.OutputFormat.VideoProfile.Bitrate = New Microsoft.Expression.Encoder.Profiles.ConstantBitrate(1000)
' Display the video device properties set
toolStripStatusLabel1.Text = sp.Size.Width.ToString() + "x" + sp.Size.Height.ToString() + " " + sp.FrameRate.ToString() + " fps"
' Sets preview window to winform panel hosted by xaml window
_deviceSource.PreviewWindow = New PreviewWindow(New HandleRef(panelVideoPreview, panelVideoPreview.Handle))
' Make this source the active one
_job.ActivateSource(_deviceSource)
btnStartStopRecording.Enabled = True
btnGrabImage.Enabled = True
toolStripStatusLabel1.Text = "Preview activated"
Else
' Gives error message as no audio and/or video devices found
MessageBox.Show("No Video/Audio capture devices have been found.", "Warning")
toolStripStatusLabel1.Text = "No Video/Audio capture devices have been found."
End If
End Sub
Private Sub btnStartStopRecording_Click(sender As System.Object, e As System.EventArgs) Handles btnStartStopRecording.Click
' Is it Recoring ?
If _bStartedRecording Then
' Yes
' Stops encoding
_job.StopEncoding()
btnStartStopRecording.Text = "Start Recording"
toolStripStatusLabel1.Text = ""
_bStartedRecording = False
Else
' Sets up publishing format for file archival type
Dim fileOut As New FileArchivePublishFormat()
' Sets file path and name
fileOut.OutputFileName = [String].Format("C:\1Invii\WebCam{0:yyyyMMdd_hhmmss}.wmv", DateTime.Now)
' Adds the format to the job. You can add additional formats as well such as
' Publishing streams or broadcasting from a port
_job.PublishFormats.Add(fileOut)
' Start encoding
_job.StartEncoding()
btnStartStopRecording.Text = "Stop Recording"
toolStripStatusLabel1.Text = fileOut.OutputFileName
_bStartedRecording = True
End If
End Sub
Private Sub btnGrabImage_Click(sender As System.Object, e As System.EventArgs) Handles btnGrabImage.Click
' Create a Bitmap of the same dimension of panelVideoPreview (Width x Height)
Using bitmap As New Bitmap(panelVideoPreview.Width, panelVideoPreview.Height)
Using g As Graphics = Graphics.FromImage(bitmap)
' Get the paramters to call g.CopyFromScreen and get the image
Dim rectanglePanelVideoPreview As Rectangle = panelVideoPreview.Bounds
Dim sourcePoints As Point = panelVideoPreview.PointToScreen(New Point(panelVideoPreview.ClientRectangle.X, panelVideoPreview.ClientRectangle.Y))
g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size)
End Using
Dim strGrabFileName As String = [String].Format("C:\1invii\Snapshot_{0:yyyyMMdd_hhmmss}.jpg", DateTime.Now)
toolStripStatusLabel1.Text = strGrabFileName
bitmap.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Jpeg)
End Using
End Sub
Private Sub GetSelectedVideoAndAudioDevices(ByRef video As EncoderDevice, ByRef audio As EncoderDevice)
video = Nothing
audio = Nothing
lblVideoDeviceSelectedForPreview.Text = ""
lblAudioDeviceSelectedForPreview.Text = ""
If lstVideoDevices.SelectedIndex < 0 OrElse lstAudioDevices.SelectedIndex < 0 Then
MessageBox.Show("No Video and Audio capture devices have been selected." & vbLf & "Select an audio and video devices from the listboxes and try again.", "Warning")
Return
End If
' Get the selected video device
For Each edv As EncoderDevice In EncoderDevices.FindDevices(EncoderDeviceType.Video)
If [String].Compare(edv.Name, lstVideoDevices.SelectedItem.ToString()) = 0 Then
video = edv
lblVideoDeviceSelectedForPreview.Text = edv.Name
Exit For
End If
Next
' Get the selected audio device
For Each eda As EncoderDevice In EncoderDevices.FindDevices(EncoderDeviceType.Audio)
If [String].Compare(eda.Name, lstAudioDevices.SelectedItem.ToString()) = 0 Then
audio = eda
lblAudioDeviceSelectedForPreview.Text = eda.Name
Exit For
End If
Next
End Sub
Private Sub StopJob()
' Has the Job already been created ?
If _job IsNot Nothing Then
' Yes
' Is it capturing ?
'if (_job.IsCapturing)
If _bStartedRecording Then
' Yes
' Stop Capturing
btnStartStopRecording.PerformClick()
End If
_job.StopEncoding()
' Remove the Device Source and destroy the job
_job.RemoveDeviceSource(_deviceSource)
' Destroy the device source
_deviceSource.PreviewWindow = Nothing
_deviceSource = Nothing
End If
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment