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. Java创建数组、赋值的四种方式,声明+创建+初始化 详解

    @ 目录 一.创建数组的四种方式 二.详解 三.数组存储的弊端 一.创建数组的四种方式 以int数据类型为例 @Test public void testNewArray() { //创建数组 //法 ...

  2. 记录CentOS 部署 express 项目

    第一步.安装 node.js1.在服务器上 /opt 下创建 node 文件夹,并进入该文件夹mkdir /opt/nodecd /opt/node 2.下载 node.js3.下载的 node.js ...

  3. Help document of CAD Plus

    中文使用帮助 Help for Mobile Update time: 2023-07-29; This article will help you how to use the CAD Plus a ...

  4. php操作sqlite3

    距离上次接触sqlite3已经快一年了,去年这篇文章讲跟着菜鸟教程学python操作sqlite3,https://www.cnblogs.com/lizhaoyao/p/13717381.html ...

  5. Redis【1】- 如何阅读 Redis源码

    1 Redis 的简介 Redis 实际上是简称,全称为 Remote Dictionary Server (远程字典服务器),由 Salvatore Sanfilippo 写的高性能 key-val ...

  6. Python 在同一/或不同PDF之间复制页面

    操作PDF文档时,复制其中的指定页面可以帮助我们从PDF文件中提取特定信息,如文本.图表或数据等,以便在其他文档中使用.复制PDF页面也可以实现在不同文件中提取页面,以创建一个新的综合文档.本文将介绍 ...

  7. Vue第三方插件

    1.滚动条 vuescroll 引入插件 - 官网 <script src="https://unpkg.com/vuescroll"></script> ...

  8. 2023 秋季学期 六周集训 Misc方向

    by 高鹏鸿.密语 写在前面,记录和交流是一个很好的习惯,建议可以自己先搭建一个博客用于存储自己的做题记录以及方便交流.还有,对于Misc方向,灵活应对十分重要,一定要善用搜索引擎. 还有一点,给大家 ...

  9. 修改QScrollArea背景色透明,且不影响子控件,在Edit Style Sheet中修改

    在QScrollArea或者父控件中设置: QScrollArea{ background-color:transparent; } 在scrollAreaWidgetContents控件或者父控件中 ...

  10. 鸿蒙应用开发从入门到入行 - 篇3:ArkUI布局基础与制作可交互页面

    鸿蒙应用开发从入门到入行 - 篇3:ArkUI布局基础与制作可交互页面 导读:在本篇文章里,您将掌握事件.装饰器.双向绑定等相关知识,并利用所学知识做一个待办列表的案例. 练手案例:登录界面 开始之前 ...