Capture from Video Source to File - C# .NET 6

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# .NET 6 Console application using the LEADTOOLS Multimedia SDK.

Overview  
Summary This tutorial covers how to capture a video file in a C# .NET 6 Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
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.

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 this 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 here: <INSTALL_DIR>\LEADTOOLS23\Bin\net

For a complete list of which DLL files are required for your application, 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.

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

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

C#
static void Main(string[] args) 
{ 
   InitLEAD(); 
   CaptureVideo(); 
} 
 
static void CaptureVideo() 
{ 
   string outputFile = @"C:\LEADTOOLS23\Resources\Images\captured.avi"; 
   CaptureCtrl capture = new CaptureCtrl(true); 
   int deviceCount = capture.VideoDevices.Count; 
   if (deviceCount < 1) 
   { 
      Console.WriteLine("No compatible devices found. Exiting."); 
      return; 
   } 
   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. 
   capture.StartCapture(CaptureMode.Video); 
 
   Console.WriteLine("Capturing to file. Press any key to stop capture..."); 
   while (!Console.KeyAvailable) 
   { 
      System.Windows.Forms.Application.DoEvents(); 
      int capMilliSeconds = (int)(1000 * capture.CaptureTime); 
      if (capMilliSeconds % 1000 == 0) // print a dot every second 
      { 
         Console.Write(". "); 
         System.Threading.Thread.Sleep(1); 
      } 
   } 
   capture.StopCapture(); 
   Console.ReadKey(true); 
 
   Console.WriteLine($"\nFinished capturing {capture.CaptureTime} seconds to file {outputFile}. Press any key to continue..."); 
   Console.ReadKey(true); 
} 

Run the Project

Note

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 capture device, then start recording to the file specified in the code, until a key is pressed.

Wrap-up

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

See Also

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

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