CapturingThread.vb
Imports System.Drawing
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Threading
Imports BytescoutScreenCapturingLib
' NOTE: if you are getting error like "invalid image" related to loading the SDK's dll then
' try to do the following:
' 1) remove the reference to the SDK by View - Solution Explorer
' then click on References, select Bytescout... reference name and right-click it and select Remove
' 2) To re-add click on the menu: Project - Add Reference
' 3) In "Add Reference" dialog switch to "COM" tab and find Bytescout...
' 4) Select it and click "Add"
' 5) Recompile the application
' Note: if you need to run on both x64 and x86 then please make sure you have set "Embed Interop Types" to True for this reference
Public Class CapturingThread
Public Shared Sub ThreadProc(ByVal obj As Object)
Dim data As CapturingThreadData = DirectCast(obj, CapturingThreadData)
data.Success = True
' Prepare Capturer:
Dim capturer As New Capturer()
capturer.RegistrationName = "demo"
capturer.RegistrationKey = "demo"
capturer.CaptureRectLeft = data.CaptureRectangle.Left
capturer.CaptureRectTop = data.CaptureRectangle.Top
capturer.CaptureRectWidth = data.CaptureRectangle.Width
capturer.CaptureRectHeight = data.CaptureRectangle.Height
capturer.OutputWidth = 640
capturer.OutputHeight = 480
' // WMV and WEBM output use WMVVideoBitrate property to control output video bitrate
' // so try to increase it by x2 or x3 times if you think the output video are you are getting is laggy
' capturer.WMVVideoBitrate = capturer.WMVVideoBitrate * 2
data.TempFile = Path.GetTempFileName()
data.TempFile = Path.ChangeExtension(data.TempFile, ".wmv")
capturer.OutputFileName = data.TempFile
capturer.CapturingType = data.CaptureType
' set border around captured area if we are not capturing entire screen
If capturer.CapturingType <> CaptureAreaType.catScreen And capturer.CapturingType <> CaptureAreaType.catWebcamFullScreen Then
capturer.CaptureAreaBorderType = CaptureAreaBorderType.cabtDashed
capturer.CaptureAreaBorderColor = CType(ColorTranslator.ToOle(Color.Red), UInteger)
End If
' Wait for events:
Dim events As WaitHandle() = New WaitHandle() {data.StartOrResumeEvent, data.PauseEvent, data.StopEvent}
Try
While (True)
Dim i As Integer = WaitHandle.WaitAny(events)
If events(i) Is data.StartOrResumeEvent Then
If Not capturer.IsRunning Then
capturer.Run()
End If
ElseIf events(i) Is data.PauseEvent Then
If capturer.IsRunning Then
capturer.Pause()
End If
ElseIf events(i) Is data.StopEvent Then
capturer.Stop()
Exit While
End If
End While
Catch ex As Exception
data.ErrorText = ex.Message
data.Success = False
Finally
' Release resources
Marshal.ReleaseComObject(capturer)
End Try
End Sub
End Class
CapturingThreadData.vb
Imports System.Drawing
Imports System.Threading
Imports BytescoutScreenCapturingLib
Public Class CapturingThreadData
Public CaptureType As CaptureAreaType
Public TempFile As String
Public CaptureRectangle As Rectangle = New Rectangle(0, 0, 320, 240)
Public Success As Boolean
Public ErrorText As String
Public StartOrResumeEvent As AutoResetEvent = New AutoResetEvent(False) ' event signalling to start or resume the recodring
Public PauseEvent As AutoResetEvent = New AutoResetEvent(False) ' event signalling to pause the recodring
Public StopEvent As AutoResetEvent = New AutoResetEvent(False) ' event signalling to stop the recording
End Class
Form1.vb
Imports System.Diagnostics
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Imports BytescoutScreenCapturingLib
' NOTE: if you are getting error like "invalid image" related to loading the SDK's dll then
' try to do the following:
' 1) remove the reference to the SDK by View - Solution Explorer
' then click on References, select Bytescout... reference name and right-click it and select Remove
' 2) To re-add click on the menu: Project - Add Reference
' 3) In "Add Reference" dialog switch to "COM" tab and find Bytescout...
' 4) Select it and click "Add"
' 5) Recompile the application
' Note: if you need to run on both x64 and x86 then please make sure you have set "Embed Interop Types" to True for this reference
Public Partial Class Form1
Inherits Form
Private _capturingThread As Thread
Private _capturingThreadData As CapturingThreadData ' data to exchange between form and capturing thread
Public Sub New()
InitializeComponent()
_capturingThreadData = New CapturingThreadData()
cmbCapturingType.SelectedIndex = 0
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Dim captureType As CaptureAreaType = CaptureAreaType.catMouse
If cmbCapturingType.SelectedIndex = 1 Then
captureType = CaptureAreaType.catScreen
End If
StartRecording(captureType)
End Sub
Private Sub btnPauseResume_Click(sender As System.Object, e As System.EventArgs) Handles btnPauseResume.Click
PauseOrResumeRecording()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
StopRecording()
End Sub
Private Sub StartRecording(ByVal captureType As CaptureAreaType)
btnStart.Enabled = False
btnPauseResume.Enabled = True
btnStop.Enabled = True
_capturingThreadData.CaptureType = captureType
' Start thread
_capturingThread = New Thread(AddressOf CapturingThread.ThreadProc)
_capturingThread.Start(_capturingThreadData)
' Signal to start the recording
_capturingThreadData.StartOrResumeEvent.Set()
End Sub
Private Sub PauseOrResumeRecording()
btnStart.Enabled = False
btnPauseResume.Enabled = True
btnStop.Enabled = True
If btnPauseResume.Text = "Pause" Then
' Signal to pause
_capturingThreadData.PauseEvent.Set()
btnPauseResume.Text = "Resume"
Else
' Signal to resume
_capturingThreadData.StartOrResumeEvent.Set()
btnPauseResume.Text = "Pause"
End If
End Sub
Private Sub StopRecording()
Cursor = Cursors.WaitCursor
' Signal to stop
_capturingThreadData.StopEvent.Set()
Try
_capturingThread.Join()
Finally
Cursor = Cursors.Default
End Try
If Not _capturingThreadData.Success Then
MessageBox.Show("Capturing failed. Error: " & _capturingThreadData.ErrorText)
Else
Dim dlg As New SaveFileDialog()
dlg.DefaultExt = "*.wmv"
dlg.Filter = "WMV files (*.wmv)|*.wmv|All files (*.*)|*.*"
dlg.FileName = "Screencast"
dlg.Title = "Save captured video as"
If dlg.ShowDialog() = DialogResult.OK Then
File.Copy(_capturingThreadData.TempFile, dlg.FileName, True)
' start the video in default associated application
Process.Start(dlg.FileName)
End If
File.Delete(_capturingThreadData.TempFile)
End If
btnStart.Enabled = True
btnPauseResume.Enabled = False
btnStop.Enabled = False
btnPauseResume.Text = "Pause"
End Sub
Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
_capturingThreadData.StopEvent.Set()
End Sub
End Class
Program.vb
Imports System.Collections.Generic Imports System.Windows.Forms NotInheritable Class Program Private Sub New() End Sub ''' <summary> ''' The main entry point for the application. ''' </summary> <STAThread> _ Friend Shared Sub Main() Application.EnableVisualStyles() Application.SetCompatibleTextRenderingDefault(False) Application.Run(New Form1()) End Sub End Class