I want to add my webcam visual to ASP.NET web page, my webcam is detected by "FilterInfoCollectionvideoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice)" Method. But When the line "VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[DropDownList1.SelectedIndex].MonikerString)" execute is return error- "Object reference not set to an instance of an object." how can I over come this problem..
Please help me to solve this problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AForge.Video;
using AForge.Video.DirectShow;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
public partial class index : System.Web.UI.Page
{
private bool DeviceExist = false;
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource = null;
protected void Page_Load(object sender, EventArgs e)
{
}
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
DropDownList1.Items.Clear();
if (videoDevices.Count == 0)
throw new ApplicationException();
DeviceExist = true;
foreach (FilterInfo device in videoDevices)
{
DropDownList1.Items.Add(device.Name);
}
DropDownList1.SelectedIndex = 0; //make dafault to first cam
}
catch (ApplicationException)
{
DeviceExist = false;
DropDownList1.Items.Add("No capture device on your system");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
getCamList();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (start.Text == "&Start")
{
if (DeviceExist)
{
videoSource = new VideoCaptureDevice(videoDevices[DropDownList1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
CloseVideoSource();
videoSource.DesiredFrameSize = new Size(160, 120);
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
Label2.Text = "Device running...";
start.Text = "&Stop";
Timer1.Enabled = true;
}
else
{
Label2.Text = "Error: No Device selected.";
}
}
else
{
if (videoSource.IsRunning)
{
Timer1.Enabled = false;
CloseVideoSource();
Label2.Text = "Device stopped.";
start.Text = "&Start";
}
}
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
//do processing here
// pictureBox1.Image = img;
}
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label2.Text = "Device running... " + videoSource.FramesReceived.ToString() + " FPS";
}
}