ByteScout Screen Capturing SDK - VB.NET - Record Screen Video From Separate Thread - ByteScout

ByteScout Screen Capturing SDK – VB.NET – Record Screen Video From Separate Thread

  • Home
  • /
  • Articles
  • /
  • ByteScout Screen Capturing SDK – VB.NET – Record Screen Video From Separate Thread

How to record screen video from separate thread in VB.NET and ByteScout Screen Capturing SDK

How to record screen video from separate thread in VB.NET

Record screen video from separate thread is easy to implement in VB.NET if you use these source codes below. Want to record screen video from separate thread in your VB.NET app? ByteScout Screen Capturing SDK is designed for it. ByteScout Screen Capturing SDK is the tool for developers who want to add screen capturing in their application. Can record screen into video and into single screenshots. Output formats are WMV, AVI, WebM for video and PNG for screenshots. You can adjust output video size, quality, resolution, framerate, video and audio codecs. Includes special privacy features for blacking out sensitive information on screen. Can also capture video from web camera, can add overlays with text or images.

This rich sample source code in VB.NET for ByteScout Screen Capturing SDK includes the number of functions and options you should do calling the API to record screen video from separate thread. Just copy and paste the code into your VB.NET application’s code and follow the instruction. You can use these VB.NET sample examples in one or many applications.

Trial version of ByteScout Screen Capturing SDK can be downloaded for free from our website. It also includes source code samples for VB.NET and other programming languages.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

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

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Screen Capturing SDK Home Page

Explore ByteScout Screen Capturing SDK Documentation

Explore Samples

Sign Up for ByteScout Screen Capturing SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

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

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Screen Capturing SDK Home Page

Explore ByteScout Screen Capturing SDK Documentation

Explore Samples

Sign Up for ByteScout Screen Capturing SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Form1.Designer.vb
      
Partial Class Form1 ''' <summary> ''' Required designer variable. ''' </summary> Private components As System.ComponentModel.IContainer = Nothing ''' <summary> ''' Clean up any resources being used. ''' </summary> ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> Protected Overrides Sub Dispose(disposing As Boolean) If disposing AndAlso (components IsNot Nothing) Then components.Dispose() End If MyBase.Dispose(disposing) End Sub #Region "Windows Form Designer generated code" ''' <summary> ''' Required method for Designer support - do not modify ''' the contents of this method with the code editor. ''' </summary> Private Sub InitializeComponent() Me.cmbCapturingType = New System.Windows.Forms.ComboBox() Me.label1 = New System.Windows.Forms.Label() Me.tableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel() Me.btnPauseResume = New System.Windows.Forms.Button() Me.btnStart = New System.Windows.Forms.Button() Me.btnStop = New System.Windows.Forms.Button() Me.tableLayoutPanel1.SuspendLayout() Me.SuspendLayout() ' 'cmbCapturingType ' Me.cmbCapturingType.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.cmbCapturingType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList Me.cmbCapturingType.FormattingEnabled = True Me.cmbCapturingType.Items.AddRange(New Object() {"Area around the mouse pointer", "Full screen"}) Me.cmbCapturingType.Location = New System.Drawing.Point(97, 12) Me.cmbCapturingType.Name = "cmbCapturingType" Me.cmbCapturingType.Size = New System.Drawing.Size(378, 21) Me.cmbCapturingType.TabIndex = 0 ' 'label1 ' Me.label1.AutoSize = True Me.label1.Location = New System.Drawing.Point(12, 15) Me.label1.Name = "label1" Me.label1.Size = New System.Drawing.Size(79, 13) Me.label1.TabIndex = 3 Me.label1.Text = "Capturing Type" ' 'tableLayoutPanel1 ' Me.tableLayoutPanel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.tableLayoutPanel1.ColumnCount = 3 Me.tableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.0!)) Me.tableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.0!)) Me.tableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 34.0!)) Me.tableLayoutPanel1.Controls.Add(Me.btnPauseResume, 0, 0) Me.tableLayoutPanel1.Controls.Add(Me.btnStart, 0, 0) Me.tableLayoutPanel1.Controls.Add(Me.btnStop, 2, 0) Me.tableLayoutPanel1.Location = New System.Drawing.Point(12, 52) Me.tableLayoutPanel1.Name = "tableLayoutPanel1" Me.tableLayoutPanel1.RowCount = 1 Me.tableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle()) Me.tableLayoutPanel1.Size = New System.Drawing.Size(463, 57) Me.tableLayoutPanel1.TabIndex = 6 ' 'btnPauseResume ' Me.btnPauseResume.Enabled = False Me.btnPauseResume.Location = New System.Drawing.Point(155, 3) Me.btnPauseResume.Name = "btnPauseResume" Me.btnPauseResume.Size = New System.Drawing.Size(143, 44) Me.btnPauseResume.TabIndex = 5 Me.btnPauseResume.Text = "Pause" Me.btnPauseResume.UseVisualStyleBackColor = True ' 'btnStart ' Me.btnStart.Location = New System.Drawing.Point(3, 3) Me.btnStart.Name = "btnStart" Me.btnStart.Size = New System.Drawing.Size(143, 44) Me.btnStart.TabIndex = 1 Me.btnStart.Text = "Start" Me.btnStart.UseVisualStyleBackColor = True ' 'btnStop ' Me.btnStop.Enabled = False Me.btnStop.Location = New System.Drawing.Point(307, 3) Me.btnStop.Name = "btnStop" Me.btnStop.Size = New System.Drawing.Size(143, 44) Me.btnStop.TabIndex = 4 Me.btnStop.Text = "Stop" Me.btnStop.UseVisualStyleBackColor = True ' 'Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(487, 121) Me.Controls.Add(Me.tableLayoutPanel1) Me.Controls.Add(Me.label1) Me.Controls.Add(Me.cmbCapturingType) Me.MaximizeBox = False Me.MinimizeBox = False Me.Name = "Form1" Me.ShowIcon = False Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Capture From Separate Thread" Me.tableLayoutPanel1.ResumeLayout(False) Me.ResumeLayout(False) Me.PerformLayout() End Sub #End Region Private cmbCapturingType As System.Windows.Forms.ComboBox Private label1 As System.Windows.Forms.Label Private WithEvents tableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel Private WithEvents btnPauseResume As System.Windows.Forms.Button Private WithEvents btnStart As System.Windows.Forms.Button Private WithEvents btnStop As System.Windows.Forms.Button End Class

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Screen Capturing SDK Home Page

Explore ByteScout Screen Capturing SDK Documentation

Explore Samples

Sign Up for ByteScout Screen Capturing SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

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

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Screen Capturing SDK Home Page

Explore ByteScout Screen Capturing SDK Documentation

Explore Samples

Sign Up for ByteScout Screen Capturing SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

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

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Screen Capturing SDK Home Page

Explore ByteScout Screen Capturing SDK Documentation

Explore Samples

Sign Up for ByteScout Screen Capturing SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Screen Capturing SDK Home Page

Explore ByteScout Screen Capturing SDK Documentation

Explore Samples

Sign Up for ByteScout Screen Capturing SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Tutorials:

prev
next