C#+AForge 实现视频录制

C#+AForge 实现视频录制

​ 在C#中,使用AForge 库实现视频录制功能是一个比较直接的过程。AForge 是一个开源的.NET框架,提供了许多用于处理图像和视频的类库。

开发步骤

安装AForge库

​ 首先,确保你的项目中已经安装了 AForge.VideoAForge.Video.FFMPEG.你可以通过NuGet包管理器来实现安装这些库:

  1. 打开你的Visual Studio 项目
  2. 转到工具->NuGet包管理器->管理解决方案的NuGet包
  3. 搜索并安装 AForgeAForge.Video 以及 AForge.Video.FFMPEG

打开与关闭摄像头

c# 复制代码
/// <summary>
/// 打开摄像头
/// </summary>
private void OpenCamera()
{
    //设置视频源并启动
    this.videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[this.comboBox1.SelectedIndex].MonikerString);
    this.videoSourcePlayer1.VideoSource = this.videoCaptureDevice;
    this.videoSourcePlayer1.Start();

    isOpen = true;

    this.btnOpen.Text = "关闭";
}


/// <summary>
/// 关闭摄像头
/// </summary>
private void CloseCamera()
{
    if (this.videoSourcePlayer1.VideoSource != null)
    {
        this.videoSourcePlayer1.SignalToStop();
        this.videoSourcePlayer1.WaitForStop();
        this.videoSourcePlayer1.VideoSource = null;

        isOpen = false;

        this.btnOpen.Text = "打开";
    }
}

实现拍照

c# 复制代码
/// <summary>
/// 拍照
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
    if (!this.isOpen)
    {
        MessageBox.Show("请先打开摄像头!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
    }

    try
    {
        //拍照
        bmp = this.videoSourcePlayer1.GetCurrentVideoFrame();

        this.pictureBox1.Image = bmp;

        if (!Directory.Exists("images"))
            Directory.CreateDirectory("images");

        string fileName = string.Format("images/图片_{0}.jpg", DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss"));

        bmp.Save(fileName);
    }
    catch (Exception ex)
    {

        MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

实现录像

c# 复制代码
/// <summary>
/// 开始录像
/// </summary>
private void StartRecord()
{
    if (!Directory.Exists("videos"))
        Directory.CreateDirectory("videos");

    string fileName = string.Format("videos/录像_{0}.avi", DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss"));
    this.videoFileName = fileName;

    // 创建视频文件写入器
    videoWriter = new VideoFileWriter();
    videoSourcePlayer1.NewFrame += VideoSourcePlayer1_NewFrame;
    videoWriter.Open(fileName, 1280, 960, 30, VideoCodec.MPEG4, 1000000); // 设置分辨率、帧率和比特率
}

/// <summary>
/// 停止录像
/// </summary>
private void StopRecord()
{
    this.CloseCamera();

    if (videoWriter != null)
    {
        videoWriter.Close();
        videoWriter = null;
    }

    this.FlushVideoPlayList();
}

实现录屏

c# 复制代码
/// <summary>
/// 开始录屏
/// </summary>
/// <param name="path"></param>
public void StartRecording(string path) //该方法需要有一个路径参数,前面我们设置过了
{
    var directory = Path.GetDirectoryName(path);
    if (!Directory.Exists(directory)) //检查路径是否存在,不存在就自动创建
    {
        Directory.CreateDirectory(directory);
    }

    try
    {
        FrmMain.videoWriter = new VideoFileWriter();
        //用于生成视频文件,参数分别为路径、视频的宽度、高度、帧数、编码格式以及视频码率
        FrmMain.videoWriter.Open(path, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, 25, VideoCodec.MPEG4, 5000000);
        recordingThread = new Thread(RecordScreen);
        recordingThread.Start();

    }
    catch (Exception err)
    {
        Console.WriteLine(err.ToString());
        MessageBox.Show("屏幕录制开启失败: " + err.Message, "提示!");
    }
}

/// <summary>
/// 停止录屏
/// </summary>
public void StopRecording()
{
    try
    {
        recordingThread.Join();
        FrmMain.videoWriter.Close();
    }
    catch (Exception err)
    {
        MessageBox.Show("屏幕录制停止失败: " + err.Message, "提示!");
        Console.WriteLine(err.ToString());
    }
}

视频播放

c# 复制代码
/// <summary>
/// 双击视频列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (this.listBox1.SelectedItem == null) return;

    this.axWindowsMediaPlayer1.URL = string.Format("videos/{0}", this.listBox1.SelectedItem.ToString());
}

object sender, MouseEventArgs e)

{

if (this.listBox1.SelectedItem == null) return;

复制代码
this.axWindowsMediaPlayer1.URL = string.Format("videos/{0}", this.listBox1.SelectedItem.ToString());

}

复制代码
# 源码下载
CSDN: [C#+AForge 实现视频下载](https://download.csdn.net/download/m0_37631902/90477114?spm=1001.2014.3001.5503)
相关推荐
咕白m6251 小时前
通过 C# 高效提取 PDF 文本的完整指南
后端·c#
hqwest4 小时前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
眠りたいです4 小时前
Qt音频播放器项目实践:文件过滤、元数据提取与动态歌词显示实现
c++·qt·ui·音视频·媒体·qt5·mime
orangapple4 小时前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
FreeDw资源库11 小时前
功能强大!开源免费的视频翻译、音视频转录工具
音视频·音视频翻译
★YUI★19 小时前
学习游戏制作记录(玩家掉落系统,删除物品功能和独特物品)8.17
java·学习·游戏·unity·c#
谷宇.20 小时前
【Unity3D实例-功能-拔枪】角色拔枪(二)分割上身和下身
游戏·unity·c#·游戏程序·unity3d·游戏开发·游戏编程
LZQqqqqo20 小时前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list
Dm_dotnet1 天前
Stylet启动机制详解:从Bootstrap到View显示
c#
三千道应用题1 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf