C# WinForm 录制屏幕
时间: 2023-07-12 17:30:40 浏览: 101
要实现屏幕录制的功能,可以使用C# WinForm中提供的Screen类和Graphics类。
以下是实现的基本步骤:
1. 引用命名空间:
```
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
```
2. 在窗体上添加一个按钮控件,并在按钮的Click事件中编写录制的代码:
```
private void btnRecord_Click(object sender, EventArgs e)
{
Rectangle bounds = Screen.PrimaryScreen.Bounds;
string filePath = Path.Combine(Application.StartupPath, "record.avi");
ProcessStartInfo psi = new ProcessStartInfo("ffmpeg.exe", "-f gdigrab -framerate 30 -offset_x " + bounds.X + " -offset_y " + bounds.Y + " -video_size " + bounds.Width + "x" + bounds.Height + " -i desktop -c:v libx264 -pix_fmt yuv420p -preset ultrafast -qp 0 " + filePath);
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process p = Process.Start(psi);
}
```
这段代码中使用了FFmpeg来实现屏幕录制,需要将FFmpeg.exe文件放在程序的同一目录下。录制的视频文件将保存为record.avi。
需要注意的是,这段代码只是一个示例,实际应用中还需要进行异常处理和优化。
希望这些信息对你有所帮助。