Playback a Video File - WinForms C# .NET 6

This tutorial shows how to use the PlayCtrl to perform simplified playback of a video file in a WinForms C# application using the LEADTOOLS Multimedia SDK.

Overview  
Summary This tutorial covers how to play a video file in a WinForms C# application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (4 KB)
Platform WinForms C# Application
IDE Visual Studio 2022
Development License Download LEADTOOLS
Try it in another language

The LEADTOOLS Multimedia Play Control contains many advanced features that simplify decoding, processing and playing back media from different sources such as files, memory buffers and network streams. The toolkit is shipped with various demos that make use of these features, such as the main Multimedia Player 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 the Playback a Video File - 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.

If using NuGet packages, the following NuGet packages are required:

If not using NuGet packages, this tutorial will require the following local DLLs, which are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:

Note

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:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Initialize the Play Control

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

In Solution Explorer, right-click on Form1.cs and select View Code to display the code-behind of the form. Add the following statements to the using block at the top.

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

Add the below code to initialize the Play Control.

C#
// Add this global variable 
private PlayCtrl _play; 

In Solution Explorer, double-click Form1.cs to display it in the Designer. Click the Events icon in the Properties Windows. Then, double-click the Load event to create an event handler.

Creating a Load event

Add the following code inside the Form1_Load event handler.

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

Add the Load and Display Image Code

Open Form1.cs in the Designer, then add a File menu with an Open menu item. To do that, open the Toolbox and double-click MenuStrip, which will add a menu to the form.

Adding a MenuStrip to the form

In the Designer, change the text of the menu to &File, which will underline the F in File. Then add an item to the menu and set its text to &Open. Leave the new item's name as openToolStripMenuItem.

Adding &Open menu item

Open the form's Designer and double-click the Open menu item to edit its event handler. Add the following code in it:

C#
private void openToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      OpenFileDialog dlg = new OpenFileDialog(); 
      dlg.InitialDirectory = @"C:\LEADTOOLS23\Resources\Media"; 
      if (dlg.ShowDialog(this) == DialogResult.OK) 
      { 
         string inputFile = dlg.FileName; 
         _play.SourceFile = inputFile; 
         _play.Run(); 
      } 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

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 allows loading and playback of any media file using the Open dialog box.

Wrap-up

This tutorial showed how to playback a video file using the PlayCtrl 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.