Nuget安装这个2个:

AForge.Controls;

AForge.Video.DirectShow;

code:

namespace WindowsFormsApp1
{
partial class FormCameraVideo
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tscbxCameras = new System.Windows.Forms.ComboBox();
this.panelVideo = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// tscbxCameras
//
this.tscbxCameras.FormattingEnabled = true;
this.tscbxCameras.Location = new System.Drawing.Point(55, 50);
this.tscbxCameras.Name = "tscbxCameras";
this.tscbxCameras.Size = new System.Drawing.Size(379, 21);
this.tscbxCameras.TabIndex = 0;
this.tscbxCameras.SelectedIndexChanged += new System.EventHandler(this.tscbxCameras_SelectedIndexChanged);
//
// panelVideo
//
this.panelVideo.Location = new System.Drawing.Point(55, 94);
this.panelVideo.Name = "panelVideo";
this.panelVideo.Size = new System.Drawing.Size(379, 282);
this.panelVideo.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 392);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 23);
this.button1.TabIndex = 2;
this.button1.Text = "btnPhotograph";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.btnPhotograph_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(335, 392);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(99, 23);
this.button2.TabIndex = 3;
this.button2.Text = "btnClose";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.btnClose_Click);
//
// FormCameraVideo
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(540, 451);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panelVideo);
this.Controls.Add(this.tscbxCameras);
this.Name = "FormCameraVideo";
this.Text = "FormCameraVideo";
this.Load += new System.EventHandler(this.FormCameraVideo_Load);
this.ResumeLayout(false); } #endregion private System.Windows.Forms.ComboBox tscbxCameras;
private System.Windows.Forms.Panel panelVideo;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
} using AForge.Controls;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging; namespace WindowsFormsApp1
{
public partial class FormCameraVideo : Form
{
public FormCameraVideo()
{
InitializeComponent();
panelVideo.Controls.Add(videoSourcePlayer);
videoSourcePlayer.Dock = DockStyle.Fill; }
private VideoSourcePlayer videoSourcePlayer = new VideoSourcePlayer();
private FilterInfoCollection videoDevices;
private void FormCameraVideo_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 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 tscbxCameras_SelectedIndexChanged(object sender, EventArgs e)
{
CameraConn();
} private void btnPhotograph_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() + "\\CameraPic" + ".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)
{
System.Windows.Forms.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;
} /// <summary>
/// Close the camera
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, EventArgs e)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop(); } }
}

  

以上是全部代码。测试笔记本电脑摄像头OK。

winform 引用AForge调用摄像头拍照的更多相关文章

  1. AForge调用摄像头拍照时设置分辨率

    简单记录下AForge2.2.5.0版本调用摄像头拍照时设置分辨率的方法. FilterInfo info = _videoDevices[0];//获取第一个摄像头 _cameraDevice = ...

  2. C#使用Aforge调用摄像头拍照

    一. 新建一个Winform项目 二.使用Nuget添加引用 安装下图中红色框住的两个程序包 安装完后发现安装了如下图的程序包,这是因为上述两个程序包存在对其它程序包的依赖. 三.编写程序 1. 窗体 ...

  3. C# - VS2019调用AForge库实现调用摄像头拍照功能

    前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...

  4. android: 调用摄像头拍照

    很多应用程序都可能会使用到调用摄像头拍照的功能,比如说程序里需要上传一张图片 作为用户的头像,这时打开摄像头拍张照是最简单快捷的.下面就让我们通过一个例子来学 习一下,如何才能在应用程序里调用手机的摄 ...

  5. vue实现PC端调用摄像头拍照人脸录入、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式

    进入正题 1. PC端调用摄像头拍照上传base64格式到后台,这个没什么花里胡哨的骚操作,直接看代码 (canvas + video) <template> <div> &l ...

  6. 【MediaKit】WPF项目中 调用摄像头拍照的开发包

    今天遇到一个 人事的项目,项目中需要调用摄像头给员工照相.如何解决这个问题呢? 介绍一个开发包给你,MediaKit.论坛里头的人都说好,但是黑兔觉得大家好才是真的好.你不妨试试~ 第一步:添加WPF ...

  7. android ——调用摄像头拍照和相册

    先在布局文件中加入两个按钮和一个图片控件 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  8. Cordova - 使用Cordova开发iOS应用实战4(调用摄像头拍照,并编辑)

    使用Cordova可以很方便的通过js代码来使用设备摄像头拍照,只需把camera插件添加进来即可. 一,添加camera插件 首先我们要在“终端”中进入工程所在的目录,然后运行如下命令: 1 cor ...

  9. 使用js调用摄像头拍照

    在一些浏览器里已经可以使用web api调用摄像头功能了. 基于此可以经行拍照摄像功能,网上找了些资料,然后实现了简单的拍照功能 演示地址 bingxl.cn/webrtc.html 代码 <! ...

  10. html5中调用摄像头拍照

    方法: getCamera: 获取摄像头管理对象 对象: Camera: 摄像头对象 CameraOption: JSON对象.调用摄像头的參数 PopPosition: JSON对象,弹出拍照或摄像 ...

随机推荐

  1. My SQL 下载和安装图文解说

    一.下载My SQL 官网下载地址:https://downloads.mysql.com/archives/installer/ 选择需要下载的版本,点击download.本文选择下载的版本是8.0 ...

  2. 人工智能模型训练技术:随机失活,丢弃法,Dropout

    前一篇:<探索训练人工智能模型的词汇大小与模型的维度> 序言:Dropout 是神经网络设计领域的一种技术,通常我们把它翻译成 随机失活 或者 丢弃法.如果训练神经网络的时候不用 Drop ...

  3. 5. Spring Cloud OpenFeign 声明式 WebService 客户端的超详细使用

    5. Spring Cloud OpenFeign 声明式 WebService 客户端的超详细使用 @ 目录 5. Spring Cloud OpenFeign 声明式 WebService 客户端 ...

  4. CSS3实现放大镜效果

    市面上基本上所有的购物平台.商城上的商品详情页,对于商品的图片都是有放大功能.那么这个功能主要是怎么实现的呢?CSS3实现放大镜效果主要依赖于CSS的一些高级特性,如transform.transit ...

  5. Apache Shiro 550反序列化漏洞复现

    目录 漏洞原理 复现 漏洞探测 方式一 ysoserial反弹shell 方式二 ShiroAttack2一键利用 修复措施 Apache Shiro 是一个用于身份验证.授权.加密和会话管理的Jav ...

  6. [昌哥IT课堂]|欢迎 MySQL 9.0,回顾 Oracle 在 8.0 版中的管理(译)

    对于新兴技术和社区的管理是相对容易的.经过 29 年发展,MySQL 已成为全球数百万用户中使用最广泛且备受信任的开源数据库之一.在这一规模的社区领导中可能存在复杂性.我们努力寻求稳定和创新的平衡,为 ...

  7. 深入Log4J源码之Log4J Core

    毕业又赶上本科的同学会,还去骑车环了趟崇明岛,六月貌似就没消停过,不过终于这些事情基本上都结束了,我也可以好好的看些书.读些源码.写点博客了. Log4J将写日志功能抽象成七个核心类/接口:Logge ...

  8. 工作流调度系统之DolphinScheduler

    Apache DolphinScheduler 是一个分布式去中心化,易扩展的可视化 DAG 工作流任务调度系统.致力于解决数据处理流程中错综复杂的依赖关系,使调度系统在数据处理流程中开箱即用. 我这 ...

  9. Jenkins篇-安装与使用

    Jenkins是一个开源自动化服务器,可以自动执行持续集成和交付软件所涉及的重复技术任务.Jenkins是基于Java的,可以从Ubuntu软件包安装,也可以通过下载和运行其Web应用程序存档(WAR ...

  10. Oracle.DataAccess.Client.OracleException: 提供程序与此版本的 Oracle 客户机不兼容

    背景:进行程序部署,客户机上原有oracle客户端的版本为2.113.1.0(以下简称113),而数据库.开发机和其他客户机上均采用的2.112.1.0(以下简称112)客户端,所以进行了替换. 卸载 ...