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. SSAS部署失败方法总结

    最近在自学SSAS,从最简单的入手,却频频遇到问题,为了以后在学习过程中能更快的进行问题的定位,所以在此将遇到的问题以及解决方案进行记录 Q1:数据源"Adventure Works DW2 ...

  2. 论文发表汇款:SWIFT code跨境汇款 —— 如何向境外账号汇款

    如何向境外账号汇款? 有以下几种方式: 对方开通中国金融产品账号或在中国有代理公司:如对方开通中国的银行卡,微信.支付宝,等等,这样其实就不属于跨境汇款了,外国的一些公司已经开设中国的金融产品和银行账 ...

  3. Linux再学!

    第三篇Linux入门 一.linux基本指令 1.Linux根目录为/,后续路径用/分隔,如/home/admin 2.Linux命令 基础格式: command: 命令本身 -options:[可选 ...

  4. 关于C++当中全局变量的释放问题

    一.由来 主要是在修改公司的一个MFC项目的时候遇到的问题,我在MFC页面的析构函数当中对一个全局图像变量进行了释放,具体如下: ai_engine_OCR::~ai_engine_OCR() { / ...

  5. Moebius for SQL Server

    Moebius(莫比斯)介绍 Moebius数据库多活集群是格瑞趋势为SQL Server数据库研发的能够同时满足可用性.数据安全.容灾.读写分离.负载均衡的一站式多活集群.集群的名字取自Moebiu ...

  6. 使用 Antlr 开发领域语言

    高 尚 (gaoshang1999@163.com), 软件工程师, 中国农业银行软件开发中心 简介: Antlr 是一个基于 Java 开发的功能强大的语言识别工具,Antlr 以其简介的语法和高速 ...

  7. 面试:10亿数据如何最快速插入MySQL?

    转载:https://mp.weixin.qq.com/s/kL1srP3FZjaTSXLULsUS5g 最快的速度把10亿条数据导入到数据库,首先需要和面试官明确一下,10亿条数据什么形式存在哪里, ...

  8. mysql之编译安装

    在CentOS7中编译安装MySQL 5.7.29 一.依赖包安装 yum install gcc gcc-c++ ncurses ncurses-devel cmake bison -y 二.下载源 ...

  9. elasticsearch-head插件基本使用

    1. 查看搜索setting信息 mp_index/_settings 2. 设置分片数量 3, 修改数据刷新间隔 { "refresh_interval": "30s& ...

  10. vue 的provide 和 inject

    1.功能说明 在开发过程中,在子组件中如何获取父组件或者祖父级的数据.这个我们之前的做法是在子组件中找到父组件实例,然后使用父组件的数据.这样其实不是很自然. 在vue 中提供了 provide 和 ...