Build a complete screen recorder app in c#
- User
Build a complete screen recorder app in c#
- Assistant
Build a complete screen recorder app in c#
Building a complete screen recorder app in C# involves capturing the screen frames and encoding them into a video format. In this guide, we'll walk through creating a simple screen recording application using C# and the .NET framework. We'll use the SharpAvi library, which allows us to record video and audio using managed code.
ScreenRecorderApp) and click OK.To handle video encoding and screen capturing, we'll use the SharpAvi library.
Right-click on the project in the Solution Explorer.
Select Manage NuGet Packages....
Search for SharpAvi and install it.
Alternatively, you can install it using the Package Manager Console:
Install-Package SharpAvi
For simplicity, we'll design a basic form with Start and Stop buttons.
btnStartStart RecordingbtnStopStop RecordingFalse (We'll enable it during recording)Your form should look something like this:
[image: Form Design] open
In your Form1.cs code-behind file, include the necessary namespaces:
using System;
using System.Drawing;
using System.Windows.Forms;
using SharpAvi;
using SharpAvi.Output;
using SharpAvi.Codecs;
using System.Threading;
We'll create a class to handle the screen recording using SharpAvi.
Add a new class to your project:
ScreenRecorder.cs.using System;
using System.Drawing;
using SharpAvi;
using SharpAvi.Output;
using SharpAvi.Codecs;
using System.Threading;
using System.Windows.Forms;
public class ScreenRecorder
{
private AviWriter writer;
private IAviVideoStream videoStream;
private Thread recordingThread;
private bool isRecording;
private int frameRate;
private Rectangle screenArea;
public ScreenRecorder(string fileName, int frameRate = 10)
{
this.frameRate = frameRate;
screenArea = Screen.PrimaryScreen.Bounds;
writer = new AviWriter(fileName)
{
FramesPerSecond = frameRate,
EmitIndex1 = true,
};
// Choose a codec (e.g., Motion JPEG)
videoStream = writer.AddMJpegWpfVideoStream(screenArea.Width, screenArea.Height, quality: 70);
videoStream.Name = "ScreenCapture";
}
public void StartRecording()
{
isRecording = true;
recordingThread = new Thread(RecordScreen)
{
IsBackground = true
};
recordingThread.Start();
}
public void StopRecording()
{
isRecording = false;
recordingThread.Join();
writer.Close();
}
private void RecordScreen()
{
var frameInterval = TimeSpan.FromSeconds(1 / (double)frameRate);
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
while (isRecording)
{
Bitmap screenshot = new Bitmap(screenArea.Width, screenArea.Height);
using (Graphics g = Graphics.FromImage(screenshot))
{
g.CopyFromScreen(screenArea.Location, Point.Empty, screenArea.Size);
}
// Convert the Bitmap to byte array
BitmapDataToByteArray(screenshot, out byte[] imageData);
screenshot.Dispose();
videoStream.WriteFrame(true, imageData, 0, imageData.Length);
var elapsed = stopwatch.Elapsed;
var delay = frameInterval - elapsed;
if (delay > TimeSpan.Zero)
Thread.Sleep(delay);
stopwatch.Restart();
}
}
private void BitmapDataToByteArray(Bitmap bitmap, out byte[] bytes)
{
// Convert bitmap to JPEG byte array
using (var ms = new System.IO.MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
bytes = ms.ToArray();
}
}
}
Bitmap to a JPEG byte array.Go back to your Form1.cs code and modify it as follows:
public partial class Form1 : Form
{
private ScreenRecorder recorder;
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
// Start recording
string fileName = $"ScreenRecord_{DateTime.Now:yyyyMMdd_HHmmss}.avi";
recorder = new ScreenRecorder(fileName, frameRate: 10);
recorder.StartRecording();
btnStart.Enabled = false;
btnStop.Enabled = true;
}
private void btnStop_Click(object sender, EventArgs e)
{
// Stop recording
recorder.StopRecording();
btnStart.Enabled = true;
btnStop.Enabled = false;
MessageBox.Show("Recording stopped.");
}
}
Don't forget to attach the event handlers to the buttons:
btnStart_Click.btnStop_Click.ScreenRecorder instance and starts recording.ScreenRecorder instance.bin\Debug) for the generated AVI file.Recording audio alongside the video involves capturing microphone or system audio input. This requires more complex handling and is beyond the scope of this basic guide. SharpAvi supports adding audio streams, and you can refer to their documentation for more details.
Modify the ScreenRecorder class to accept a custom screen area instead of the full screen. You can use a Rectangle to specify the area.
public ScreenRecorder(string fileName, Rectangle area, int frameRate = 10)
{
this.frameRate = frameRate;
screenArea = area;
// Rest of the initialization...
}
frameRate to balance between video smoothness and performance impact.Add try-catch blocks around critical sections to handle exceptions gracefully.
You've now built a basic screen recorder application in C#. This application captures the screen and saves it as an AVI video file. While this is a simple implementation, you can enhance it by adding features like audio recording, custom codecs, user interface improvements, and more robust error handling.
Note: Always ensure you have the necessary permissions to capture and record the screen, especially when distributing or using the application in environments with sensitive information.
Not yet included in a published release.