Build a c# screen recorder in c# using latest library
- User
Build a c# screen recorder in c# using latest library
- Assistant
Build a c# screen recorder in c# using latest library
Creating a screen recorder in C# involves capturing the screen's frames and encoding them into a video format. One of the latest and efficient libraries you can use for this purpose is SharpAvi, which allows you to record AVI files with ease.
Below is a step-by-step guide to building a simple screen recorder using SharpAvi in a Windows Forms application.
ScreenRecorder) and choose the desired location.Design a simple UI with the following components:
You can drag and drop these buttons from the Toolbox onto your form and name them appropriately (e.g., btnStart, btnStop).
Below is the complete code to implement the screen recorder. This example captures the primary screen and saves it as an AVI file.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SharpAvi;
using SharpAvi.Output;
using SharpAvi.Codecs;
namespace ScreenRecorder
{
public partial class Form1 : Form
{
private AviWriter _writer;
private IAviVideoStream _videoStream;
private Timer _timer;
private Rectangle _screenBounds;
private int _frameRate = 10; // Frames per second
public Form1()
{
InitializeComponent();
InitializeRecording();
}
private void InitializeRecording()
{
// Define the screen bounds (Primary screen)
_screenBounds = Screen.PrimaryScreen.Bounds;
// Initialize the AviWriter
_writer = new AviWriter("recording.avi")
{
FramesPerSecond = _frameRate,
EmitIndex1 = true,
};
// Add a video stream using MJPEG codec
_videoStream = _writer.AddVideoStream();
_videoStream.Width = _screenBounds.Width;
_videoStream.Height = _screenBounds.Height;
_videoStream.Codec = KnownFourCcCodes.CodecMJpeg;
_videoStream.BitsPerPixel = 32;
// Initialize the Timer
_timer = new Timer();
_timer.Interval = 1000 / _frameRate; // in milliseconds
_timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
CaptureAndWriteFrame();
}
private void CaptureAndWriteFrame()
{
using (Bitmap bmp = new Bitmap(_screenBounds.Width, _screenBounds.Height, PixelFormat.Format32bppArgb))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(_screenBounds.Left, _screenBounds.Top, 0, 0, _screenBounds.Size, CopyPixelOperation.SourceCopy);
}
// Convert Bitmap to byte array (BGR32)
var frame = BitmapToBgr32(bmp);
_videoStream.WriteFrame(true, frame, 0, frame.Length);
}
}
private byte[] BitmapToBgr32(Bitmap bitmap)
{
BitmapData bmpData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
int length = bmpData.Stride * bmpData.Height;
byte[] pixelData = new byte[length];
Marshal.Copy(bmpData.Scan0, pixelData, 0, length);
bitmap.UnlockBits(bmpData);
// Convert ARGB to BGR0
for (int i = 0; i < pixelData.Length; i += 4)
{
byte temp = pixelData[i];
pixelData[i] = pixelData[i + 2];
pixelData[i + 2] = temp;
pixelData[i + 3] = 0; // Set alpha to 0
}
return pixelData;
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
btnStop.Enabled = true;
_timer.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
btnStop.Enabled = false;
btnStart.Enabled = true;
_timer.Stop();
_writer.Close();
MessageBox.Show("Recording saved as recording.avi");
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (_timer.Enabled)
{
_timer.Stop();
_writer.Close();
}
base.OnFormClosing(e);
}
}
}
Initialization:
Capturing Frames:
User Interface Events:
Form Closing:
recording.avi in the application's directory._frameRate variable as needed.While SharpAvi is a robust choice, there are other libraries and methods you can explore:
Choose the library that best fits your project's requirements and your familiarity with the technology.
By following the steps above, you've created a simple screen recorder in C# using the SharpAvi library. This foundation can be expanded with additional features such as audio recording, multiple display support, UI enhancements, and more sophisticated encoding options.
Feel free to customize and enhance the application to fit your specific needs!