Search code examples
c#webcamqr-codewebcam-capture

Capture images from webcam in c#


I've been working on an application which generates and reads(decode) QR codes. In decoding part the user capture the picture of QR code and the program will start process of decoding. My problem is I don't Know how I can take the photo. P.S: If you offer a library please give a link which contains the tutorial of using that lib. Thank you.


Solution

  • I have been looking for web cam recording since a long time, you can use Aforge.NET .

    Here is code for the same using WPF :

    public partial class MainWindow : Window
        {
            private FilterInfoCollection VideoCaptureDevices;
            private VideoCaptureDevice FinalVideo;
            public VideoFileWriter writer= new VideoFileWriter();
    
            public MainWindow()
            {
                InitializeComponent();
    
                VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
                {
                    comboBox1.Items.Add(VideoCaptureDevice.Name);
                }
                comboBox1.SelectedIndex = 0;
    
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                writer.Open(@"d:\\newVid.avi", 640, 480, 25, VideoCodec.MPEG4);
    
                FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
                FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
                FinalVideo.Start();
    
            }
    
            void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
            {
    
                System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone();
    
                Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();
    
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
    
                MemoryStream ms = new MemoryStream();
                imgforms.Save(ms, ImageFormat.Bmp);
                ms.Seek(0, SeekOrigin.Begin);
    
                bi.StreamSource = ms;
                bi.EndInit();            
    
                //Using the freeze function to avoid cross thread operations 
                bi.Freeze();
    
                //Calling the UI thread using the Dispatcher to update the 'Image' WPF control         
                Dispatcher.BeginInvoke(new ThreadStart(delegate
                {
                    pictureBox1.Source = bi; /*frameholder is the name of the 'Image' WPF control*/
                }));
    
                for (int i = 0; i < 2; i++)
                {
                    writer.WriteVideoFrame(bmp);
                }
    
            }
    
            private void Stop_Click(object sender, RoutedEventArgs e)
            {
                writer.Close();
                FinalVideo.Stop();
                this.Close();
            }
    
        }
    

    include below namespaces:

    using AForge.Video;
    using AForge.Video.DirectShow;
    using AForge.Video.FFMPEG;
    using System.Drawing.Drawing2D;
    using AForge.Video.VFW;
    

    You can set change frame rate as per your convenience.