C#利用網路攝影機拍照功能實現

來源:互聯網
上載者:User

標籤:

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.Threading;using System.Diagnostics;using AForge;using AForge.Video;using AForge.Controls;using AForge.Video.DirectShow;using AForge.Video.FFMPEG;using AForge.Imaging;using AForge.Imaging.Filters;using System.IO;namespace Camera{    public partial class Form1 : Form    {        private FilterInfoCollection videoDevices;        private VideoCaptureDevice videoSource;        private bool stopREC = true;        private bool createNewFile = true;        private string videoFileFullPath = string.Empty; //視頻檔案全路徑        private string imageFileFullPath = string.Empty; //影像檔全路徑        private string videoPath = @"E:\video\"; //視頻檔案路徑        private string videoFileName = string.Empty; //視頻檔案名稱        private string imageFileName = string.Empty; //影像檔名        private string drawDate = string.Empty;        private VideoFileWriter videoWriter = null;        public delegate void MyInvoke(); //定義一個委託方法        string g_s_AutoSavePath = AppDomain.CurrentDomain.BaseDirectory + "Capture\\";        object objLock = new object(); //定義一個對象的鎖        int frameRate = 20; //預設幀率        private Stopwatch stopWatch = null;        IVideoSource iVideoSource = null;        private int flag = 0;        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            try            {                // 枚舉所有視頻輸入裝置                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();        }        private void CameraConn()        {            videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);            videoSource.DesiredFrameSize = new Size(320, 240);            videoSource.DesiredFrameRate = 1;            videoPlayer.VideoSource = videoSource;            videoPlayer.Start();        }        private void toolStripButton2_Click(object sender, EventArgs e)        {            videoPlayer.SignalToStop();            videoPlayer.WaitForStop();        }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)        {            toolStripButton2_Click(null, null);        }        private void button2_Click(object sender, EventArgs e)        {            try            {                flag = 0;                videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);                            }            catch (Exception ex)            {                MessageBox.Show("捕獲映像失敗!" + ex.Message, "提示");            }        }        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)        {            Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();            if (flag == 0)            {                string img = "E://img//" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";                bitmap.Save(img);                MessageBox.Show("ok");                flag = 1;            }        }           }}

  具體:1.調用各個dll檔案

              2.AForge.Controls產生自身控制項videoPlayer

C#利用網路攝影機拍照功能實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.