C# 使用aforge.net调用本地摄像头画面和抓拍图片
在日常开发过程中,c#调用本地摄像头由很多种方式可以实现,今天我们就来讲一下如何通过Aforge图像处理库来调用本地摄像头并实现抓拍功能。
首先,我们新建一个空白的Winform程序,在Nuget包管理器中搜索Aforge,会出现很多结果,其中安装AForge.Video.DirectShow和AForge.Controls两个库即可。
AForge.Video.DirectShow和AForge.Controls两个库安装好以后,我们先生成一下项目,目的是确保Aforge自定义控件能够正确地添加到VS控件工具箱中,如下图所示
最后我们开始实现代码逻辑,先全局定义FilterInfoCollection、VideoCaptureDevice、VideoCapabilities三个变量,然后具体代码流程如下。
- 在窗体加载Load事件时,扫描摄像头信息并显示在下拉列表中。
- 切换摄像头列表时根据选中的摄像头信息获取支持的分辨率列表
- 点击连接按钮开始连接指定摄像头,并把摄像头画面显示在控件中
- 点击抓拍按钮获取摄像头画面当前帧保存到程序根目录
- 点击断开连接按钮断开与摄像头的连接
至此整个代码逻辑梳理完毕,下面附上Aforge调用摄像头示例代码。
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AforgeExp
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoDevice;
private VideoCapabilities[] videoCapabilities;
private void FrmMain_Load(object sender, EventArgs e)
{
//获取内置、外接摄像头,非网络摄像头
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count != 0)
{
foreach (FilterInfo device in videoDevices)
{
cbo_camera.Items.Add(device.Name);
}
}
else
{
cbo_camera.Items.Add("没有找到摄像头");
}
cbo_camera.SelectedIndex = 0;
}
private void cbo_camera_SelectedIndexChanged(object sender, EventArgs e)
{
if (videoDevices.Count != 0)
{
//获取每个摄像头支持的分辨率
videoDevice = new VideoCaptureDevice(videoDevices[cbo_camera.SelectedIndex].MonikerString);
GetDeviceResolution(videoDevice);
}
}
/// <summary>
/// 获取每个摄像头支持的分辨率
/// </summary>
/// <param name="videoCaptureDevice"></param>
private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice)
{
cbo_rate.Items.Clear();
videoCapabilities = videoCaptureDevice.VideoCapabilities;
foreach (VideoCapabilities capabilty in videoCapabilities)
{
cbo_rate.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");
}
cbo_rate.SelectedIndex = 0;
}
private void btn_conn_Click(object sender, EventArgs e)
{
if (videoDevice != null)
{
if ((videoCapabilities != null) && (videoCapabilities.Length != 0))
{
videoDevice.VideoResolution = videoCapabilities[cbo_rate.SelectedIndex];
myvideo.VideoSource = videoDevice;
myvideo.Start();
btn_conn.Enabled = false;
btn_stop.Enabled = true;
btn_cap.Enabled = true;
}
}
}
private void btn_stop_Click(object sender, EventArgs e)
{
if (myvideo.VideoSource != null)
{
myvideo.SignalToStop();
myvideo.WaitForStop();
myvideo.VideoSource = null;
btn_conn.Enabled = true;
btn_stop.Enabled = false;
btn_cap.Enabled = false;
}
}
private void btn_cap_Click(object sender, EventArgs e)
{
if (myvideo.VideoSource != null)
{
Bitmap img = myvideo.GetCurrentVideoFrame();
img.Save("001.jpg");
MessageBox.Show("抓拍成功,文件在程序根目录下。");
}
}
/// <summary>
/// 窗体关闭程序退出时断开摄像头连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (myvideo.VideoSource != null)
{
myvideo.SignalToStop();
myvideo.WaitForStop();
myvideo.VideoSource = null;
btn_conn.Enabled = true;
btn_stop.Enabled = false;
btn_cap.Enabled = false;
}
}
}
}
最后附上示例文章中的源代码下载地址: AforgeExp.zip (访问密码: 7831)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?