Capturing images with webcam in c#

I need some help for one part of my project. I need webcam to capture 3 images in 2-seconds intervals triggered by motion detector. Here is my code. The problem is I have three times the same image (the first one).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Threading.Tasks;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Data.SqlClient;
using System.Drawing;
using System.Threading;

namespace pro
{
    class Program
    {
        static SerialPort serialPort = new SerialPort("COM4", 9600);
        static FilterInfoCollection WebcamColl;
        static VideoCaptureDevice Device;
        static int no = 1;

        static void Main(string[] args)
        {
             if (!serialPort.IsOpen)
                serialPort.Open();   
             Console.WriteLine("");             
             serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
             Console.ReadKey();            
       }
       static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {          
             string _read =  serialPort.ReadExisting().ToString();
             if (_read == "ERROR" )
             {
                 WebcamColl = new FilterInfoCollection(FilterCategory.VideoInputDevice);            
                 Device = new VideoCaptureDevice(WebcamColl[0].MonikerString);
                 Device.Start();
                 Device.NewFrame += new NewFrameEventHandler( Device_NewFrame);            
                 Console.ReadLine();        
             }
        }
        static void Device_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {           
             for (int i = 0; i < 3; i++)
             {
                 System.Drawing.Image img = (Bitmap)eventArgs.Frame.Clone();
                 string imagePath;
                 string fileName = "Image";
                 fileName = fileName + no.ToString() + ".jpg";
                 img.Save("D:aaaa" + fileName);
                 Thread.Sleep(2000);
                 Console.WriteLine("Snapshot Saved.");
                 imagePath = "D:aaaa" + fileName;           
                 FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);               
                 BinaryReader br = new BinaryReader(fs);
                 byte[] _image = br.ReadBytes((int)fs.Length);
                 br.Close();
                 fs.Close();         
                 SqlConnection conn = new SqlConnection("Data source=ULTRASLANSQLEXPRESS; Initial Catalog=veritabani; Integrated Security=true");
                 SqlCommand kmt = new SqlCommand("insert into _imageler(_image,kullanici_id,tarih) Values (@image,22,getdate())", conn);
                 kmt.Parameters.Add("@image", SqlDbType.Image, _image.Length).Value = _image;

                 try
                 {
                     conn.Open();
                     kmt.ExecuteNonQuery();
                     Console.WriteLine(" Saving image to the database successfull..");
                     no++;
                 }

                 catch (Exception ex)
                {
                     Console.WriteLine(ex.Message.ToString());
                }

               finally
               {
                     conn.Close();
               }

           }
           Device.SignalToStop();
       }

    }
}
链接地址: http://www.djcxy.com/p/81042.html

上一篇: 在没有预览视图或cameraUI的情况下捕获摄像头图像?

下一篇: 用c#捕捉摄像头的图像