开发者

基于C#实现屏幕录制功能

目录
  • 前言
  • 环境准备
  • 创建 C# 程序
  • 录屏代码
    • 代码解释
  • 总结

    前言

    在许多应用场景中,屏幕录制是一项非常有用的功能。

    不管是用于教学、演示、故障排查还是游戏录制,能够方便快捷地记录屏幕上的活动都是非常有价值的。

    本文将详细介绍如何使用 C# 实现屏幕录制功能,帮助大家快速掌握这一实用技能。

    环境准备

    在开始之前,请确保您已经安装了以下软件:

    1、Visual Studio

    用于php C# 开发的集成开发环境。

    2、FFmpeg

    下载并解压 FFmpeg,记录下其可执行文件的路径。

    创建 C# 程序

    1、创建项目

    打开 Visual Studio,新建一个 Windows Forms 应用程序项目,命名为 AppRecordScreen

    2、添加控件

    在窗口中添加两个按钮,分别用于"开始录制"和"停止录制"。

    录屏代码

    接下来,需要编写录屏的核心代码。

    以下是实现屏幕录制的完整代码:

    using System.Diagnostics;
    
    namespace AppRecordScreen
    {
        public partial class Form1 : Form
        {
            // 录屏相关变量  
            privatebool isRecording = false;
            private Process ffmpegProcess;
            privatestring outputVideoPath;
            // FFmpeg可执行文件的完整路径  
            privatestring ffmpegPath = 
            @"D:\Software\ffmpeg-master-latest-win64-gpl-shared
            \bin\ffmpeg.exe"; 
            public Form1()
            {
                InitializeComponent();
            }
    
            private async void btnStart_Click(object sender, EventArgs e)
            {
                if (!isRecording)
                {
                    StartRecording();
                }
            }
    
            private void btnStop_Clpythonick(object sender, EventArgs e)
            {
                if (isRecording)
                {
                    StopRecording();
                }
            }
            /// <summary>  
            /// 开始录制屏幕  
            /// </summary>  
            private void StartRecording()
            {
                // 生成唯一的视频文件名  
                outputVidandroideoPath = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                    $"ScreenRecord_
                    {DateTime.Now:yyyyMMdd_HHmmss}.mp4"
                );
    
                try
                {
                    // 使用FFmpeg进行屏幕录制  
                    ffmpegPjsrocess = new Process
                    {
                        StartInfo = new ProcessStartInfo
                        {
                            // 使用完整路径  
                            FileName = ffmpegPath,
                            Arguments = $"-f gdigrab -framerate 30 -i desktop 
                            -c:v libx264 -preset ultrafast {outputVideoPath}",
                            UseShellExecute = false,
                            RedirectStandardInput = true,
                            CreateNoWindow = true
                        }
                    };
    
                    ffmpegProcess.Start();
                    isRecording = true;
    
                    MessageBox.Show("录制已开始", "提示", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"启动录制失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
            /// <summary>  
            /// 停止录制屏幕  
            /// </summary>  
            private void StopRecording()
            {
                try
                {
                    // 发送 'q' 命令给 FFmpeg 进程以正常结束录制  
                    ffmpegProcess.StandardInput.WriteLine("q");
                    ffmpegProcess.WaitForExit();
    
                    isRecording = false;
    
                    MessageBox.Show($"录制已完成。视频保存在:{outputVideoPath}", "提示", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"停止录制失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
            // 在关闭窗体时确保停止录制  
            protected override void OnFormClosing(FormClosingEventArgs e)
            {
                if (isRecording)
                {
                    StopRecording();
                }
            }
        }
    }
    

    基于C#实现屏幕录制功能

    代码解释

    变量定义

    主要定义了录屏相关的变量,包括录制状态、FFmpeg 进程、输出视频路径和 FFmpeg 的可执行文件路径。

    开始录制

    当用户点击"开始录制"按钮时,StartRecording 方法将被调用,生成一个带有时间戳的唯一视频文件名,并启动 FFmpeg 进程进行屏幕录制。

    停止录制

    用户点击"停止录制"按钮时,StopRecording 方法会发送结束命令给 FFmpeg 进程,停止录制并保存视频。

    关闭窗体

    在窗体关闭时,如果正在录制,会自动停止录制,确保视频完整保存。

    总结

    通过本文的学习掌握如何使用 C# 实现屏幕录制功能,并能够根据实际需求进行扩展和优化。还可以在此基础上不断扩展功能,例如添加视频格式选择、录音功能等。

    希望能通过js这篇文章大家能够快速上手并开发自己的屏幕录制工具。

    到此这篇关于基于C#实现屏幕录制功能的文章就介绍到这了,更多相关C#屏幕录制内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜