Creating a LEADTOOLS Multimedia TV Tuner Capture Application

Perform the following steps to create and run a multimedia TV tuner capture application using the LEADTOOLS Multimedia CaptureCtrl control.

  1. Start Visual Studio .NET 2010.

  2. Choose File->New->Project... from the menu.

  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Forms Application" in the Templates List.

  4. Type the project name as "Multimedia TV Tuner Capture" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.

  5. In the "Solution Explorer" window, right-click the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and select Leadtools.Multimedia.dll and click OK. Also, right-click the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the "COM" tab and select:

    • LEAD MPEG-2 Encoder Filter Library

    Note: The above COM object must be registered. If it was not use regsvr32. For more information, refer to DirectShow Registry-free Activation.

    Next click OK.

  6. Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and drag a CaptureCtrl control on the form. NOTE: If you do not have CaptureCtrl in your toolbox, select Tools->Choose Toolbox Items from the menu. Click Browse and then select Leadtools.Multimedia.dll from "<LEADTOOLS_INSTALLDIR>\Bin\Dotnet\Win32" and then click Open and then click OK. After adding it to the form, set the following properties on the capture control:

    Property Value
    Name _capturectrl
    Anchor Top, Bottom, Left, Right
  7. Go to the toolbox (View->Toolbox) and drag a ProgressBar control (to be used to display capture progress) on the form (below the capture control) and set the following properties:

    Property Value
    Name _progress
    Anchor Bottom, Left, Right
  8. Go to the toolbox (View->Toolbox) and drag 4 Button controls on the bottom of the form (for Capture, TV Tuner properties, Channel Up and Channel Down) and set the following properties:

    Property Value
    Name _buttonCapture
    Text Capture
    Anchor Bottom, Right
    Name _buttonTuner
    Text TV Tuner...
    Anchor Bottom, Right
    Name _buttonChannelUp
    Text Up
    Anchor Bottom, Right
    Name _buttonChannelDown
    Text Down
    Anchor Bottom, Right
  9. Go to the toolbox (View->Toolbox) and drag a Label control, a Text control, another Label control and a ProgressBar control on the bottom left of the form (for a "Channel:" label, a channel text box, a "Signal:" label and a signal strength meter) and set the following properties:

    Property Value
    Name label1
    Text Channel:
    Anchor Bottom, Left
    Name _textChannel
    Anchor Bottom, Left
    Locked True
    ReadOnly True
    TextAlign Center
    Name label2
    Text Signal:
    Anchor Bottom, Left
    Name _signal
    Anchor Bottom, Left
  10. Switch Form1 to code view (right-click Form1 in the Solution Explorer then choose View Code) and add the following lines to the beginning of the file:

    VB
    Imports System.Runtime.InteropServices 
    Imports Leadtools.Multimedia 
    Imports LMMPEG2EncoderLib 
                      
         

    C#
    using System.Runtime.InteropServices; 
    using Leadtools.Multimedia; 
    using LMMPEG2EncoderLib; 
                      
         

  11. Declare the following private variables:

    VB
    Private _targetFile As String 
    Private _captureTime As Integer 
                      
         

    C#
    private string _targetFile; 
    private int _captureTime; 
                      
         

  12. Add an event handler to the Form1 Load event and code it as follows:

    VB
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load 
       If _capturectrl.VideoDevices.Count = 0 Then 
          Throw New Exception("No capture devices available") 
       End If 
                     
       If _capturectrl.VideoDevices("Analog") Is Nothing Then 
          Throw New Exception("No Analog TV capture devices available") 
       End If 
                     
       _capturectrl.VideoDevices("Analog").Selected = True 
       _capturectrl.UseVideoDeviceAudio = True 
                         
       _capturectrl.VideoCompressors.Mpeg2.Selected = True 
       _capturectrl.AudioCompressors.MpegAudio.Selected = True 
       _targetFile = "capture.avi" 
       _captureTime = 30 
       _capturectrl.PreviewSource = CapturePreview.VideoAndAudio 
       _capturectrl.Preview = True 
                     
       SetCompressorProperties() 
       DisplayChannelAndSignal() 
    End Sub 
                      
         

    C#
    private void Form1_Load(object sender, EventArgs e) 
    { 
       if (_capturectrl.VideoDevices.Count == 0) 
          throw new Exception("No capture devices available"); 
                     
       if (_capturectrl.VideoDevices["Analog"] == null) 
          throw new Exception("No Analog TV capture devices available"); 
                     
       _capturectrl.VideoDevices["Analog"].Selected = true; 
       _capturectrl.UseVideoDeviceAudio = true; 
                         
       _capturectrl.VideoCompressors.Mpeg2.Selected = true; 
       _capturectrl.AudioCompressors.MpegAudio.Selected = true; 
       _targetFile = @"capture.avi"; 
       _captureTime = 30; 
       _capturectrl.PreviewSource = CapturePreview.VideoAndAudio; 
       _capturectrl.Preview = true; 
                     
       SetCompressorProperties(); 
       DisplayChannelAndSignal(); 
    } 
                      
         

  13. Add the following helper methods as follows:

    VB
    Private Sub SetCompressorProperties() 
       If _capturectrl.VideoCompressors.Mpeg2.Selected = True Then 
          Dim mpeg2Encoder As LMMPEG2Encoder = TryCast(_capturectrl.GetSubObject(CaptureObject.VideoCompressor), LMMPEG2Encoder) 
          If Not mpeg2Encoder Is Nothing Then 
             mpeg2Encoder.EncodingThreads = eMpeg2EncodingThreads.MPEG2_THREAD_AUTO 
             If Not _capturectrl.TVTuner Is Nothing Then 
                Select Case _capturectrl.TVTuner.TVFormat 
                   Case AnalogVideoStandard.NTSC_433, AnalogVideoStandard.NTSC_M, AnalogVideoStandard.NTSC_M_J 
                      mpeg2Encoder.VideoFormat = eMPEG2VIDEOFORMAT.MPEG2_VF_NTSC 
                   Case AnalogVideoStandard.PAL_60, AnalogVideoStandard.PAL_B, AnalogVideoStandard.PAL_D, AnalogVideoStandard.PAL_G, AnalogVideoStandard.PAL_H, AnalogVideoStandard.PAL_I, AnalogVideoStandard.PAL_M, AnalogVideoStandard.PAL_N, AnalogVideoStandard.PAL_N_COMBO 
                      mpeg2Encoder.VideoFormat = eMPEG2VIDEOFORMAT.MPEG2_VF_PAL 
                   Case Else 
                      mpeg2Encoder.VideoFormat = eMPEG2VIDEOFORMAT.MPEG2_VF_NTSC 
                End Select 
             End If 
                     
             Marshal.ReleaseComObject(mpeg2Encoder) 
          End If 
       End If 
    End Sub 
                     
    Private Sub DisplayChannelAndSignal() 
       If Not _capturectrl.TVTuner Is Nothing Then 
          Dim currChannel As Integer = _capturectrl.TVTuner.Channel 
          _textChannel.Text = currChannel.ToString() 
          Dim signal As TunerSignalStrength = _capturectrl.TVTuner.SignalPresent 
          Select Case signal 
             Case TunerSignalStrength.NoSignal 
                _signal.Value = 0 
             Case TunerSignalStrength.HasNoSignalStrength 
                _signal.Value = 10 
             Case TunerSignalStrength.SignalPresent 
                _signal.Value = 100 
          End Select 
       End If 
    End Sub 
                     
    Private Sub ChangeChannel(ByVal up As Boolean) 
       Dim chanMin As Integer = _capturectrl.TVTuner.ChannelMin 
       Dim chanMax As Integer = _capturectrl.TVTuner.ChannelMax 
       Dim currChannel As Integer = _capturectrl.TVTuner.Channel 
       Dim currVideoChannel As Integer = _capturectrl.TVTuner.VideoSubChannel 
                     
       If up Then 
          If currChannel = chanMax Then 
             currChannel = chanMin 
          Else 
             currChannel += 1 
          End If 
       Else 
          If currChannel = chanMin Then 
             currChannel = chanMax 
          Else 
             currChannel -= 1 
          End If 
       End If 
       _capturectrl.TVTuner.SetChannel(currChannel, -1, -1) 
                     
       DisplayChannelAndSignal() 
    End Sub 
                      
         

    C#
    private void SetCompressorProperties() 
    { 
       if (_capturectrl.VideoCompressors.Mpeg2.Selected == true) 
       { 
          LMMPEG2Encoder mpeg2Encoder = _capturectrl.GetSubObject(CaptureObject.VideoCompressor) as LMMPEG2Encoder; 
          if (mpeg2Encoder != null) 
          { 
             mpeg2Encoder.EncodingThreads = eMpeg2EncodingThreads.MPEG2_THREAD_AUTO; 
             if (_capturectrl.TVTuner != null) 
             { 
                switch (_capturectrl.TVTuner.TVFormat) 
                { 
                   default: 
                   case AnalogVideoStandard.NTSC_433: 
                   case AnalogVideoStandard.NTSC_M: 
                   case AnalogVideoStandard.NTSC_M_J: 
                      mpeg2Encoder.VideoFormat = eMPEG2VIDEOFORMAT.MPEG2_VF_NTSC; 
                      break; 
                   case AnalogVideoStandard.PAL_60: 
                   case AnalogVideoStandard.PAL_B: 
                   case AnalogVideoStandard.PAL_D: 
                   case AnalogVideoStandard.PAL_G: 
                   case AnalogVideoStandard.PAL_H: 
                   case AnalogVideoStandard.PAL_I: 
                   case AnalogVideoStandard.PAL_M: 
                   case AnalogVideoStandard.PAL_N: 
                   case AnalogVideoStandard.PAL_N_COMBO: 
                      mpeg2Encoder.VideoFormat = eMPEG2VIDEOFORMAT.MPEG2_VF_PAL; 
                      break; 
                } 
             } 
                     
             Marshal.ReleaseComObject(mpeg2Encoder); 
          } 
       } 
    } 
                     
    private void DisplayChannelAndSignal() 
    { 
       if (_capturectrl.TVTuner != null) 
       { 
          int currChannel = _capturectrl.TVTuner.Channel; 
          _textChannel.Text = currChannel.ToString(); 
          TunerSignalStrength signal = _capturectrl.TVTuner.SignalPresent; 
          switch (signal) 
          { 
             case TunerSignalStrength.NoSignal: 
                _signal.Value = 0; 
                break; 
             case TunerSignalStrength.HasNoSignalStrength: 
                _signal.Value = 10; 
                break; 
             case TunerSignalStrength.SignalPresent: 
                _signal.Value = 100; 
                break; 
          } 
       } 
    } 
                     
    private void ChangeChannel(bool up) 
    { 
       int chanMin = _capturectrl.TVTuner.ChannelMin; 
       int chanMax = _capturectrl.TVTuner.ChannelMax; 
       int currChannel = _capturectrl.TVTuner.Channel; 
       int currVideoChannel = _capturectrl.TVTuner.VideoSubChannel; 
                     
       if (up) 
       { 
          if (currChannel == chanMax) 
             currChannel = chanMin; 
          else 
             currChannel++; 
       } 
       else 
       { 
          if (currChannel == chanMin) 
             currChannel = chanMax; 
          else 
             currChannel--; 
       } 
       _capturectrl.TVTuner.SetChannel(currChannel, -1, -1); 
                     
       DisplayChannelAndSignal(); 
    } 
                      
         

  14. Add an event handler to the _capturectrl Progress event and code it as follows:

    VB
    Private Sub _capturectrl_Progress(ByVal sender As System.Object, ByVal e As ProgressEventArgs) Handles _capturectrl.Progress 
       _progress.Value = CInt((_progress.Maximum * e.time) / (_captureTime * 1000)) 
    End Sub 
                      
         

    C#
    private void _capturectrl_Progress(object sender, ProgressEventArgs e) 
    { 
       _progress.Value = (int)((_progress.Maximum * e.time) / (_captureTime * 1000)); 
    } 
                      
         

  15. Add an event handler to the _capturectrl Complete event and code it as follows:

    VB
    Private Sub _capturectrl_Complete(ByVal sender As System.Object, ByVal e As EventArgs) Handles _capturectrl.Complete 
       MessageBox.Show("Capture Complete") 
       _buttonCapture.Enabled = True 
    End Sub 
                      
         

    C#
    private void _capturectrl_Complete(object sender, EventArgs e) 
    { 
       MessageBox.Show("Capture Complete"); 
       _buttonCapture.Enabled = true; 
    } 
                      
         

  16. Add an event handler to the _buttonTuner Click event and code it as follows:

    VB
    Private Sub _buttonTuner_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _buttonTuner.Click 
       If Not _capturectrl.TVTuner Is Nothing Then 
          If _capturectrl.HasDialog(CaptureDlg.TVTuner) Then 
             _capturectrl.ShowDialog(CaptureDlg.TVTuner, Me) 
          End If 
                     
          DisplayChannelAndSignal() 
       End If 
    End Sub 
                      
         

    C#
    private void _buttonTuner_Click(object sender, EventArgs e) 
    { 
       if (_capturectrl.TVTuner != null) 
       { 
          if (_capturectrl.HasDialog(CaptureDlg.TVTuner)) 
             _capturectrl.ShowDialog(CaptureDlg.TVTuner, this); 
                     
          DisplayChannelAndSignal(); 
       } 
    } 
                      
         

  17. Add an event handler to the _buttonChannelUp Click event and code it as follows:

    VB
    Private Sub _buttonChannelUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _buttonChannelUp.Click 
       If Not _capturectrl.TVTuner Is Nothing Then 
          ChangeChannel(True) 
       End If 
    End Sub 
                      
         

    C#
    private void _buttonChannelUp_Click(object sender, EventArgs e) 
    { 
       if (_capturectrl.TVTuner != null) 
       { 
          ChangeChannel(true); 
       } 
    } 
                      
         

  18. Add an event handler to the _buttonChannelDown Click event and code it as follows:

    VB
    Private Sub _buttonChannelDown_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _buttonChannelDown.Click 
       If Not _capturectrl.TVTuner Is Nothing Then 
          ChangeChannel(False) 
       End If 
    End Sub 
                      
         

    C#
    private void _buttonChannelDown_Click(object sender, EventArgs e) 
    { 
       if (_capturectrl.TVTuner != null) 
       { 
          ChangeChannel(false); 
       } 
    } 
                      
         

  19. Add an event handler to the _buttonCapture Click event and code it as follows:

    VB
    Private Sub _buttonCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _buttonCapture.Click 
       Try 
          _capturectrl.Preview = True 
          _capturectrl.TargetFile = _targetFile 
          _capturectrl.UseTimeLimit = True 
          _capturectrl.TimeLimit = _captureTime 
                            
          _capturectrl.StartCapture(CaptureMode.VideoAndAudio) 
          _buttonCapture.Enabled = False 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub    
                      
         

    C#
    private void _buttonCapture_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          _capturectrl.Preview = true; 
          _capturectrl.TargetFile = _targetFile; 
          _capturectrl.UseTimeLimit = true; 
          _capturectrl.TimeLimit = _captureTime; 
                            
          _capturectrl.StartCapture(CaptureMode.VideoAndAudio); 
          _buttonCapture.Enabled = false; 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(ex.Message); 
       } 
    } 
                      
         

  20. Build, and Run the program to test it.

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Multimedia