FrmTest.cs 3.1 KB
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeTest
{
    public partial class FrmTest : Form
    {
        public FrmTest()
        {
            InitializeComponent();
        }

        private FilterInfoCollection videoDevices;
        private void FrmTest_Load(object sender, EventArgs e)
        {
            //var zipHelper = new CompressHelper();
            //zipHelper.CompressSingle(@"D:\Data Matrix ECC 200.dcm");
            //List<string> strList = new List<string>() { @"D:\Data Matrix ECC 200.dcm", @"D:\ilmergeTest.txt", @"D:\QR Code.dcm" };
            //zipHelper.CompressMulti(strList.ToArray(), @"D:\test.gz");
            //zipHelper.DeCompressMulti(@"D:\test.gz", @"D:\web\");


            comboBox1.Items.Clear();

            this.videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo v in videoDevices)
            {
                comboBox1.Items.Add(v.Name);
            }
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedIndex = 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int index = comboBox1.SelectedIndex;
            if (this.videoDevices.Count > index)
            {
                VideoCaptureDevice source = new VideoCaptureDevice(this.videoDevices[index].MonikerString);
                this.videoSourcePlayer.SignalToStop();
                this.videoSourcePlayer.WaitForStop();
                this.videoSourcePlayer.VideoSource = source;
                this.videoSourcePlayer.Start();
            }
            comboBox1.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            videoSourcePlayer.Stop();
            comboBox1.Enabled = true;
        }
        private int flag = 0;
        private void btnGetImage_Click(object sender, EventArgs e)
        {
            if (videoSourcePlayer.IsRunning)
            {
                flag = 0;
                videoSourcePlayer.NewFrame +=  video_NewFrame;
                //string path = Application.StartupPath + @"\ImagePath\Image.bmp";
                Bitmap bit = (Bitmap)(videoSourcePlayer.GetCurrentVideoFrame()).Clone() ;
                //bit.Save(path, ImageFormat.Bmp);
                //MessageBox.Show("成功保存图片至:" + path);
            }
        }
        private void video_NewFrame(object sender, ref Bitmap image)
        {
            Bitmap bitmap = (Bitmap)image.Clone();
            if (flag == 0)
            {
                string dir = Application.StartupPath + @"/ImagePath/ ";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                string img = dir + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
                bitmap.Save(img);
                flag = 1;
            }
        }
    }
}