首页 > 代码库 > C#调用摄像头拍照

C#调用摄像头拍照

在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#调用摄像头拍照