找传奇、传世资源到传世资源站!

C#摄像头源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C#摄像头源码 C#语言基础-第1张 C#摄像头源码
using 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

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