C# 开启摄像头 拍照
生活随笔
收集整理的這篇文章主要介紹了
C# 开启摄像头 拍照
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
引用 Aforge
Aforge 介紹:http://baike.baidu.com/link?url=T3XRoneXNTVU8Hoh2tNOtiBYXCPBYmslpVUWWPw4c2pxyclFtmMgAlTNlBhLiXoLN_GamxUpiI3x-UBAErSUNa
獲得攝像設備
public class FaceCommon{#region 方法/// <summary>/// 獲取已插USB攝像頭硬件Id/// </summary>/// <returns></returns>public static List<string> GetCameraDeviceId(){List<string> _cameraList = new List<string>();FilterInfoCollection _filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);// 過濾信息 獲取所有已插USB攝像頭驅動信息if (_filterInfoCollection != null && _filterInfoCollection.Count > 0){for (int i = 0; i < _filterInfoCollection.Count; i++){_cameraList.Add(_filterInfoCollection[i].MonikerString); //向集合中添加USB攝像頭硬件Id }_cameraList.Remove(""); //移出空項return _cameraList;}else{return null;}}#endregion} /// <summary>/// 開始播放/// </summary> private void StartCamera(){if (_videoCaptureDevice != null){_videoCaptureDevice.Start();}}/// <summary>/// 停止播放/// </summary>private void StopCamera(){if (_videoCaptureDevice != null){_videoCaptureDevice.SignalToStop();}}/// <summary>/// 初始化攝像頭/// </summary>private void InitialCamera(){if (this.comboBox1.SelectedItem != null){_videoCaptureDevice = new VideoCaptureDevice(this.comboBox1.SelectedItem.ToString());_videoCaptureDevice.NewFrame += HandNewFrame;}}private void HandNewFrame(object sender, NewFrameEventArgs args){try{this.Invoke(new Action(() => ?//拉姆達表達式 =〉后面跟著函數體{if (args != null){this.pictureBox1.Image = args.Frame.Clone() as Image;imgobj = args.Frame.Clone() as Image;}}));}catch (Exception){//throw; }}開始攝像:
private void button1_Click(object sender, EventArgs e){InitialCamera();StartCamera();}?拍照并將圖片保存:
StopCamera();pictureBox1.BackgroundImage = imgobj;if (pictureBox1.BackgroundImage != null){Bitmap bmp = new Bitmap(pictureBox1.BackgroundImage);if (sm.textBox1.Text== String.Empty){MessageBox.Show("請先完善人員的基本信息", "警告");}else{String number = sm.textBox1.Text.Trim();String dir = System.Environment.CurrentDirectory + "\\Images\\" + number + ".bmp";if (File.Exists(@dir)){File.Delete(@dir);bmp.Save("Images//" + number + ".bmp");bmp.Dispose(); //記得釋放 不然會提醒文件被另一進程使用。 }else{bmp.Save("Images//" + number + ".bmp");bmp.Dispose();}if (File.Exists(@dir)){ sm.pictureBox1.Image = Image.FromFile(dir);sm.pictureBox1.ImageLocation = @dir;}else { // }}}界面截圖:
轉載于:https://www.cnblogs.com/2714585551summer/p/5360514.html
總結
以上是生活随笔為你收集整理的C# 开启摄像头 拍照的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zookeeper+kafka集群安装之
- 下一篇: 快速开发框架,及库存管理系统,基于eas