欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

C#摄像头拍照 示例源码

介绍 评论 失效链接反馈

from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing.Imaging;using System.Text;using System.Windows;using System.Windows.Forms;using System.IO;using System.Windows.Media.Imaging;using AForge;using AForge.Controls;using AForge.Video;using AForge.Video.DirectShow;using Size = System.Drawing.Size;namespace OperateCamera{ public partial class Form1 : Form { private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource; public Form1() { InitializeComponent(); } private void Form1_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; } } 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(); } //关闭摄像头 private void btnClose_Click(object sender, EventArgs e) { videoSourcePlayer.SignalToStop(); videoSourcePlayer.WaitForStop(); } //主窗体关闭 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { btnClose_Click(null, null); } //拍照 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 picName = GetImagePath() "\\" "xiaosy" ".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; } }}

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复