標籤: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#調用網路攝影機拍照