中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

c# 使用AForge.NET操作攝像頭操作代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
c# 使用AForge.NET操作攝像頭操作代碼
AForge.NET是一個專門為開發(fā)者和研究者基于C#框架設(shè)計的,這個框架提供了不同的類庫和關(guān)于類庫的資源,還有很多應(yīng)用程序例子,包括計算機(jī)視覺與人工智能,圖像處理,神經(jīng)網(wǎng)絡(luò),遺傳算法,機(jī)器學(xué)習(xí),機(jī)器人等領(lǐng)域。 根據(jù)網(wǎng)上的方法,運(yùn)行程序攝像頭出來了,但是怎么截圖呢?找到一個顯示圖片方法如下

private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)  
       {  
           Bitmap img = (Bitmap)eventArgs.Frame.Clone();  
           //do processing here  
           pictureBox1.Image = img;  
       }  
開始參考這句代碼寫了:
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)  
       {  
           Bitmap img = (Bitmap)eventArgs.Frame.Clone();  
           string imgPath = dirc + "/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";  
           img.Save(imgPath);  
       }  

但是點(diǎn)擊截圖程序就自動不停截了大量的圖片,最后導(dǎo)致程序出問題。想了一個笨方法,可以只截一次圖:

 private int flag = 1;    
/// <summary>  
        /// 截圖  
        /// </summary>  
        private void toolStripButton3_Click(object sender, EventArgs e)  
        {  
            flag = 0;  
            videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);  
        }        
        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)  
        {  
            Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();  
            if (flag == 0)  
            {  
                string img = dirc + "/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";  
                bitmap.Save(img);  
                flag = 1;      
            }  
        }         

完整的程序代碼如下:
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Drawing.Imaging;  
using System.Text;  
using System.Windows.Forms;  
using System.IO;  
using AForge;  
using AForge.Video;  
using AForge.Video.DirectShow;  
using AForge.Imaging;  
using AForge.Imaging.Filters;  
  
namespace Camera  
{  
    public partial class Form1 : Form  
    {  
        private FilterInfoCollection videoDevices;  
        private VideoCaptureDevice videoSource;  
        private int flag = 1;  
        private string dirc = System.AppDomain.CurrentDomain.BaseDirectory + "Images"; //截圖保存的目錄  
                 
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            if (!Directory.Exists(dirc))  
                Directory.CreateDirectory(dirc);  
  
            try  
            {  
                // 枚舉所有視頻輸入設(shè)備  
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);  
                if (videoDevices.Count == 0)  
                    throw new ApplicationException();  
  
                foreach (FilterInfo device in videoDevices)  
                {  
                    tscbxCameras.Items.Add(device.Name);  
                }  
  
                tscbxCameras.SelectedIndex = 0;  
            }  
            catch (ApplicationException)  
            {  
                tscbxCameras.Items.Add("No local capture devices");  
                videoDevices = null;  
            }  
        }  
  
        private void toolStripButton1_Click(object sender, EventArgs e)  
        {  
            CameraConn();  
        }  
  
        /// <summary>  
        /// 連接開啟攝像頭  
        /// </summary>  
        private void CameraConn()  
        {  
            videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);  
            videoSource.DesiredFrameSize = new Size(500, 300);  
            videoSource.DesiredFrameRate = 1;  
            videPlayer.VideoSource = videoSource;  
            videPlayer.Start();  
        }  
        /// <summary>  
        /// 關(guān)閉攝像頭  
        /// </summary>  
        private void toolStripButton2_Click(object sender, EventArgs e)  
        {  
            videPlayer.SignalToStop();  
            videPlayer.WaitForStop();  
        }  
  
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
        {  
            toolStripButton2_Click(null, null);  
        }  
        /// <summary>  
        /// 截圖  
        /// </summary>  
        private void toolStripButton3_Click(object sender, EventArgs e)  
        {  
            flag = 0;  
            videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);  
        }        
        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)  
        {  
            Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();  
            if (flag == 0)  
            {  
                string img = dirc + "/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";  
                bitmap.Save(img);  
                flag = 1;      
            }  
        }         
    }  
}  

標(biāo)簽: 代碼 開發(fā)者 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C#控制臺進(jìn)行文件讀寫的簡單范例

下一篇:C#實(shí)現(xiàn)的算24點(diǎn)游戲的算法