Capture, Archive, and Play Live Streams - WinForms C# .NET 6

This tutorial shows how to capture, store, and play videos from live streams in a WinForms C# .NET 6 application using the LEADTOOLS Multimedia SDK.

Overview  
Summary This tutorial shows how to capture, store, and play videos from live streams in a WinForms C# .NET 6 application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform Windows WinForms C# Application
IDE Visual Studio 2022
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Capture, Archive, and Play Live Streams - WinForms C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. For this tutorial the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\net:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the Capture and Play Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, double-click Form1.cs to display the Designer. Open the Toolbox and double-click MenuStrip to add a menu to the form. Add a &File dropdown menu item to the new MenuStrip. Add two menu items to the dropdown, &Start Capture and &Play. Adding & will underline the first letter of the menu item. Respectively, leave the new items' names as playToolStripMenuItem and captureToolStripMenuItem. Double-click on both the &Start Capture menu item and the &Play menu item to create their respective click events.

If one is not added yet, go back to the Form Designer and add a new Load event for the Form named Form1_Load by double clicking the Form. Doing this will bring up the code behind Form1. Add the using statements below to the top.

C#
using System; 
using System.IO; 
using System.Windows.Forms; 
 
using Leadtools; 
using Leadtools.Multimedia; 

Add the following global variables to the Form1 class.

C#
private PlayCtrl playCtrl; 
private CaptureCtrl captureCtrl; 
private string captureFile = @"INPUT CAPTURE AND PLAY FILE LOCATION"; 
string availableDevices; 
int option = 0; 

Add the code below to the Form1_Load event to initialize and add the PlayCtrl to the Form.

C#
private void Form1_Load(object sender, EventArgs e) 
{ 
    playCtrl = new PlayCtrl(); 
    playCtrl.Dock = DockStyle.Fill; 
    Controls.Add(playCtrl); 
    playCtrl.BringToFront(); 
} 

Add the following code to the captureToolStripMenuItem_Click event to start capturing the live stream video and save it to the captureFile file path.

C#
private void captureToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
    playCtrl.Stop(); 
    playCtrl.ResetSource(); 
    availableDevices = " "; 
 
    captureCtrl = new CaptureCtrl(); 
    captureCtrl.TargetFile = captureFile; 
    captureCtrl.TargetFormat = TargetFormatType.AVI; 
 
    // Select a suitable video compressor 
    captureCtrl.VideoCompressors.MJpeg.Selected = true; 
    // Select a suitable audio compressor if available 
    // CaptureCtrl.AudioCompressors.AC3.Selected = true; 
    int deviceCount = captureCtrl.VideoDevices.Count; 
 
    if (deviceCount < 1) 
    { 
        Console.WriteLine("No compatible devices found. Exiting."); 
        return; 
    } 
 
    for (int n = 0; n < deviceCount; n++) 
    { 
        availableDevices += n.ToString() + " - " + captureCtrl.VideoDevices[n].FriendlyName + "\n"; 
    } 
    availableDevices += "\nFor this demo we will be using a connected USB device"; 
 
    MessageBox.Show(availableDevices, "Available Devices"); 
    captureCtrl.VideoDevices.Selection = option; 
 
    //use CaptureMode.VideoAndAudio if an audio device is also selected. 
 
    DialogResult dialogResult = MessageBox.Show("Press OK to Begin capturing", "Start Capture", MessageBoxButtons.OK); 
    if (dialogResult == DialogResult.OK) 
    { 
        captureCtrl.StartCapture(CaptureMode.Video); 
    } 
 
    dialogResult = MessageBox.Show("Press OK to End capturing", "Stop Capturing", MessageBoxButtons.OK); 
    if (dialogResult == DialogResult.OK) 
    { 
        captureCtrl.StopCapture(); 
    } 
} 

Add the lines of code below to the playToolStripMenuItem_Click event to play the captured video in the captureFile file path.

C#
private void playToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
    playCtrl.SourceFile = captureFile; 
    playCtrl.Run(); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and the form should appear. To test, follow the steps below:

  1. Click on File -> Start Capture to connect to the USB device and start capturing the live stream.

  2. Click on File -> Play to start playing the captured live stream.

Wrap-up

This tutorial showed how to capture a live stream, save it to file, and play it back using the CaptureCtrl and PlayCtrl classes.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.