C#調用網路攝影機拍照

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

在winforn程式中,經常會遇到一些調用硬體的功能,這裡給大家講解的事使用AForge調用網路攝影機

首先引用用dll檔案

這些都是需要應用的dll檔案,其中AForge.Controls.dll檔案裡面封裝了一些void控制項,在工具箱中應用檔案就會出現如下控制項

這裡會使用到VideoSourcePlayer控制項,下面是底層代碼

using引用 

using System.Drawing.Imaging;
using System.Windows;
using System.IO;
using System.Windows.Media.Imaging;
using AForge;
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;

 private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource;public FrmOperateCamera()        {            InitializeComponent();        }        /// <summary>        /// 表單載入        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void FrmOperateCamera_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;            }        }        /// <summary>        /// 串連        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnConnect_Click(object sender, EventArgs e)        {            CameraConn();        }        //串連網路攝影機        private void CameraConn()        {            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);            videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);            videoSource.DesiredFrameRate = 1;            videoSourcePlayer.VideoSource = videoSource;            videoSourcePlayer.Start();        }        /// <summary>        /// 關閉        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnClose_Click(object sender, EventArgs e)        {            videoSourcePlayer.SignalToStop();            videoSourcePlayer.WaitForStop();        }        /// <summary>        /// 拍照        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Photograph_Click(object sender, EventArgs e)        {            try            {                if (videoSourcePlayer.IsRunning)                {                    BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(                                    videoSourcePlayer.GetCurrentVideoFrame().GetHbitmap(),                                    IntPtr.Zero,                                    Int32Rect.Empty,                                    BitmapSizeOptions.FromEmptyOptions());                    PngBitmapEncoder pE = new PngBitmapEncoder();                    pE.Frames.Add(BitmapFrame.Create(bitmapSource));                    string pa = DateTime.Now.ToString("yyyyMMddHHmmss");                    Random rd = new Random();                    pa += rd.Next(9);                    string picName = GetImagePath() + "\\" + pa + ".jpg";                    if (File.Exists(picName))                    {                        File.Delete(picName);                    }                    using (Stream stream = File.Create(picName))                    {                        pE.Save(stream);                    }                    //拍照完成後關網路攝影機並重新整理同時關表單                    if (videoSourcePlayer != null && videoSourcePlayer.IsRunning)                    {                        videoSourcePlayer.SignalToStop();                        videoSourcePlayer.WaitForStop();                    }                    this.Close();                }            }            catch (Exception ex)            {                MessageBox.Show("網路攝影機異常:" + ex.Message);            }        }        private string GetImagePath()        {            string personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)                         + Path.DirectorySeparatorChar.ToString() + "PersonImg";            if (!Directory.Exists(personImgPath))            {                Directory.CreateDirectory(personImgPath);            }            return personImgPath;        }
View Code

這裡還需要添加一些程式引用,不然還會報錯,WIndowsBase,System.Xaml,PresentationCore。

表單效果如下,顯示部分使用的是VideoSourcePlayer控制項。

以上就是實際效果。

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.