C# screen recorder [closed]

Posted on


After a while of working on it, I’ve finally got my screen recorder to work in C#. I’m looking for suggestions to improve the video quality as well as clean up the code a bit. Specifically I want to create a method that resets everything after the screen recorder is finished, but I can’t think of an efficient way to do that. Any help in these regards is appreciated!

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Accord.Video.FFMPEG;
using System.IO;
using System.Runtime.InteropServices;

namespace WorkTracker
class ScreenRecorder
//Video variables:
private readonly Rectangle bounds;
private readonly string outputPath = "";
private readonly string tempPath = "";
private int fileCount = 1;
private List<string> inputImageSequence = new List<string>();

//Audio variables:
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

public ScreenRecorder(Rectangle b, string outPath)
//Create temporary folder for screenshots:
if (Directory.Exists("D://"))
string pathName = "D://tempScreenCaps";
tempPath = pathName;
string pathName = "C://Documents//tempScreenCaps";
tempPath = pathName;

this.bounds = b;
outputPath = outPath;

private static void DeletePath(string target_dir)
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);

//Delete each screenshot:
foreach (string file in files)
File.SetAttributes(file, FileAttributes.Normal);

//Delete the path:
foreach (string dir in dirs)

Directory.Delete(target_dir, false);

public void RecordVideo()
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
using (Graphics g = Graphics.FromImage(bitmap))
//Add screen to bitmap:
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
//Save screenshot:
string name = tempPath + "//screenshot-" + fileCount + ".jpeg";
bitmap.Save(name, ImageFormat.Jpeg);

//Dispose of bitmap:

public void RecordAudio()
record("open new Type waveaudio Alias recsound", "", 0, 0);
record("record recsound", "", 0, 0);

public void Stop()
int width = bounds.Width;
int height = bounds.Height;

var framRate = 15;

//Save audio:
string audioPath = "save recsound " + outputPath + "//mic.wav";
record(audioPath, "", 0, 0);
record("close recsound", "", 0, 0);

string finalAudioPath = outputPath + "//mic.wav";
string finalVideoPath = outputPath + "//video.mp4";

using (VideoFileWriter vFWriter = new VideoFileWriter())
//Create new video file:
vFWriter.Open(outputPath + "//video.mp4", width, height, framRate, VideoCodec.MPEG4);

//Make each screenshot into a video frame:
foreach (string imageLocation in inputImageSequence)
Bitmap imageFrame = System.Drawing.Image.FromFile(imageLocation) as Bitmap;

//Delete the screenshots and temporary folder:

//Add audio:
string args = "/c ffmpeg -i "video.mp4"" -i ""mic.wav"" -shortest outPutFile.mp4""


Leave a Reply

Your email address will not be published. Required fields are marked *