How to capture the video from webcam in asp net c


ASP.NET is a popular programming used for web applications. One common requirement in web applications is capturing video from a webcam. In this article, we will explore how to capture video from a webcam in ASP.NET using C#.


Before we begin, make sure you have the following:

  • ASP.NET installed
  • A webcam connected to your computer

Step 1: Setting up the ASP.NET Project

First, let's a new ASP.NET project. Open Visual Studio and select “New Project”. Choose the ASP.NET Web template and provide a name for your project. Click “OK” to create the project.

Step 2: Adding the Required Libraries

In order to capture video from a webcam, we need to add the necessary libraries to our project. Right-click on the project in the Solution Explorer and select “Manage NuGet Packages”. Search for “AForge.Video” and install the .

Step 3: Creating the Web Form

Next, let's create a new web form where we will the captured video. Right-click on the project and select “Add” > “Web Form”. Provide a name for the web form and click “Add”.

Step 4: Adding the HTML

In the newly created web form, add the following HTML markup:

Step 5: Capturing Video from Webcam

Now, let's write the C# code to capture video from the webcam. In the code-behind file of the web form, add the following code:

using AForge.Video;
using AForge.Video.DirectShow;

protected void Page_Load(object sender, EventArgs e)
    FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
    videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);

private void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
    System.Drawing.Image img = (System.Drawing.Bitmap)eventArgs.Frame.Clone();
    using (MemoryStream ms = new MemoryStream())
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] imageBytes = ms.ToArray();
        string base64String = Convert.ToBase64String(imageBytes);
        videoElement.Src = "data:image/jpeg;base64," + base64String;


In the code above, we first import the required namespaces for capturing video. Then, in the Page_Load event, we retrieve the available video devices and select the first device. We subscribe to the NewFrame event, which is triggered whenever a new frame is captured from the webcam. In the event , we convert the captured frame to a base64 string and set it as the source of the video element in the HTML markup.


In this article, we have learned how to capture video from a webcam in ASP.NET using C#. By following the steps outlined above, you can easily webcam video capture functionality into your ASP.NET web application.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents