天天看點

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

在項目裡有個功能需要調取螢幕實作攝像功能,在網上找到了一些Demo雖然也沒看太懂,但是基本功能還是做到了,在此做個筆記。

本文源碼下載下傳位址:https://download.csdn.net/download/qq_42757964/12259466

AForge引用類包下載下傳位址:https://download.csdn.net/download/qq_42757964/12255300

簡單示例:

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能
C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

下面進行詳細步驟說明:

1,首先我們建立Monitorr項目:

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

2.将需要的dll檔案複制到Debug目錄下。

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

3.将引用添加進項目(選擇浏覽找到你剛剛存放的對應位置的.dll檔案)

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能
C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能
C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

4.添加成功後,在右側工具欄建立一個Camera檔案夾(也可以不建立直接将這些别人寫好的自己定義控件拖拽到這裡),将其拖拽過來。這一步的目的是将dll檔案轉化為可直接拖拽到Form視窗的控件

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能
C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

拖拽過來之後你就能直接看到控件了,但據部分同學反映有的是拖拽不過來,有的是拖拽過來之後并沒有變成控件形式,而是變成了一個個文本檔案像下面這樣:

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

因為每個VS版本或者電腦系統有差異性,在此我也不能完全料想到有哪些錯誤,是以大家如果做到這一步發現出現某些問題的話,請自行百度 谷歌。在這給大家例舉一些我在網上看到的一些方法吧~(僅供參考):

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能
C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能
C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能
C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

5.總之正常來說,拖拽到工具欄之後,就你就能看到:

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

那麼接下來,你需要把VideoSourcePlayer控件拖拽到視窗上。

在拖拽我們需要的三個button和一個combobox

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

6.我們計劃在button1的事件中去搜尋目前裝置連接配接上的所有監控設施,并将其列舉在下拉清單中。

工具類:

public FilterInfoCollection GetDevices()
        {
            try
            {
                //枚舉所有視訊輸入裝置
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (videoDevices.Count != 0)
                {
                    // LogClass.WriteFile("已找到視訊裝置.");
                    MessageBox.Show("已經找到裝置");
                    return videoDevices;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                // LogClass.WriteFile("error:沒有找到視訊裝置!具體原因:" + ex.Message);
                MessageBox.Show("沒有找到視訊裝置");

                return null;
            }
        }

           

Button1點選事件:

// 枚舉所有視訊輸入裝置

                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                if (videoDevices.Count == 0)

                    throw new ApplicationException();

                foreach (FilterInfo device in videoDevices)

                {

                    comboBox1.Items.Add(device.Name);

                }

                comboBox1.SelectedIndex = 0;
}

           

Button2中去打開你在清單中選擇的裝置:

//連接配接攝像頭

        private void CameraConn()

        {

            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);

            videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);

            videoSource.DesiredFrameRate = 1;

            videoSourcePlayer1.VideoSource = videoSource;

            videoSourcePlayer1.Start();

        }


           

Button3中選擇讓其停止且等待:

videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();//而非徹底關閉

           

然後就OK啦!(在下拉搜尋清單裡,你能看的你目前裝置上所有連結到的監控設施,ps:因為目前我的筆記本上隻有這一個攝像頭是以就索引到了這一個~):

C#用Aforge.net類庫調用攝像頭實作監控或者拍照功能

完整代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;

using System.Drawing.Imaging;

namespace Monitorr
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        //監控設施的選擇
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoSource;



        public int selectedDeviceIndex = 0;



        public FilterInfoCollection GetDevices()
        {
            try
            {
                //枚舉所有視訊輸入裝置
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (videoDevices.Count != 0)
                {
                    // LogClass.WriteFile("已找到視訊裝置.");
                    MessageBox.Show("已經找到裝置");
                    return videoDevices;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                // LogClass.WriteFile("error:沒有找到視訊裝置!具體原因:" + ex.Message);
                MessageBox.Show("沒有找到視訊裝置");

                return null;
            }
        }


        //連接配接攝像頭

        private void CameraConn()

        {

            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);

            videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);

            videoSource.DesiredFrameRate = 1;

            videoSourcePlayer1.VideoSource = videoSource;

            videoSourcePlayer1.Start();

        }
        private void button1_Click(object sender, EventArgs e)
        {
            try

            {

                // 枚舉所有視訊輸入裝置

                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                if (videoDevices.Count == 0)

                    throw new ApplicationException();



                foreach (FilterInfo device in videoDevices)

                {

                    comboBox1.Items.Add(device.Name);

                }

                comboBox1.SelectedIndex = 0;

            }

            catch (ApplicationException)

            {

                comboBox1.Items.Add("No local capture devices");

                videoDevices = null;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "打開監控")
            {
                if (comboBox1.Text != null)
                {
                    CameraConn();
                }
                else
                {
                    MessageBox.Show("---------");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            videoSourcePlayer1.SignalToStop();

            videoSourcePlayer1.WaitForStop();
        }
    }
}

           

參考部落格:

https://blog.csdn.net/special00/article/details/82629916

https://blog.csdn.net/chenhongwu666/article/details/40594365

https://blog.csdn.net/zwb_578209160/article/details/80309377