Capture from Video Source to File - Console C#

This tutorial shows how to use the CaptureCtrl to capture video from a video source, such as a webcam, and store it into a disk file in a C# Windows Console application using the LEADTOOLS Multimedia SDK.

Summary This tutorial covers how to capture a video file in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform Windows C# Console Application
IDE Visual Studio 2019, 2022
Development License Download LEADTOOLS
Try it in another language

The LEADTOOLS Multimedia Capture Control contains many advanced features that simplify the process of capturing video from devices and cameras. The toolkit is shipped with various demos that make use of these features, such as the main Multimedia Capture demo that has different editions in multiple programming languages.

This tutorial shows how to use the Capture Control to perform simplified capturing of a video file.

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 from Video Source to File - Console 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.

This tutorial requires the following local DLLs, which are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64


Different SDK features require different references. For a complete list, refer to Multimedia Files You Must Include 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 Video Code

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

In Solution Explorer, open Program.cs, then add using Leadtools; and using Leadtools.Multimedia; to the using block at the top.

// Using block at the top 
using System.Windows.Forms; 
using Leadtools; 
using Leadtools.Multimedia; 

Add a new method called CaptureVideo(), and call it in the Main method after the UnlockMultimedia() method call.

static void Main(string[] args) 
static void CaptureVideo() 
   string outputFile = @"C:\LEADTOOLS22\Resources\Images\captured.avi"; 
   CaptureCtrl capture = new CaptureCtrl(true); 
   int deviceCount = capture.VideoDevices.Count; 
   if (deviceCount < 1) 
      Console.WriteLine("No compatible devices found. Exiting."); 
   Console.WriteLine("Select device by typing its number and pressing Enter:"); 
   for (int n = 0; n < deviceCount; n++) 
      Console.WriteLine(n.ToString() + " : " + capture.VideoDevices[n].FriendlyName); 
   int deviceIndex = int.Parse(Console.ReadLine()); 
   Console.WriteLine("Preparing to capture . ."); 
   capture.VideoDevices.Selection = deviceIndex; 
   capture.TargetFile = outputFile; 
   capture.TargetFormat = TargetFormatType.AVI; 
   // select a suitable compressor 
   capture.VideoCompressors.MJpeg.Selected = true; 
   //use CaptureMode.VideoAndAudio if an audio device is also selected. 
   Console.WriteLine("Capturing to file. Press any key to stop capture..."); 
   while (!Console.KeyAvailable) 
      int capMilliSeconds = (int)(1000 * capture.CaptureTime); 
      if (capMilliSeconds % 1000 == 0) // print a dot every second 
         Console.Write(". "); 
   Console.WriteLine($"\nFinished capturing {capture.CaptureTime} seconds to file {outputFile}. Press any key to continue..."); 

Run the Project

Make sure there's at least one video source present on the test PC, such as a camera device. Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application will prompt the user to select a video device, then start recording to the file specified in the code, until a key is pressed.


This tutorial showed how to select a video source and capture from it using the CaptureCtrl class.

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.