C# screen recorder
up vote
1
down vote
favorite
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";
Directory.CreateDirectory(pathName);
tempPath = pathName;
}
else
{
string pathName = "C://Documents//tempScreenCaps";
Directory.CreateDirectory(pathName);
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);
File.Delete(file);
}
//Delete the path:
foreach (string dir in dirs)
{
DeletePath(dir);
}
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);
inputImageSequence.Add(name);
fileCount++;
//Dispose of bitmap:
bitmap.Dispose();
}
}
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;
vFWriter.WriteVideoFrame(imageFrame);
imageFrame.Dispose();
}
//Close:
vFWriter.Close();
}
//Delete the screenshots and temporary folder:
DeletePath(tempPath);
//Add audio:
string args = "/c ffmpeg -i "video.mp4" -i "mic.wav" -shortest outPutFile.mp4";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = @"" + outputPath;
startInfo.Arguments = args;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
}
}
c# performance video
New contributor
add a comment |
up vote
1
down vote
favorite
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";
Directory.CreateDirectory(pathName);
tempPath = pathName;
}
else
{
string pathName = "C://Documents//tempScreenCaps";
Directory.CreateDirectory(pathName);
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);
File.Delete(file);
}
//Delete the path:
foreach (string dir in dirs)
{
DeletePath(dir);
}
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);
inputImageSequence.Add(name);
fileCount++;
//Dispose of bitmap:
bitmap.Dispose();
}
}
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;
vFWriter.WriteVideoFrame(imageFrame);
imageFrame.Dispose();
}
//Close:
vFWriter.Close();
}
//Delete the screenshots and temporary folder:
DeletePath(tempPath);
//Add audio:
string args = "/c ffmpeg -i "video.mp4" -i "mic.wav" -shortest outPutFile.mp4";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = @"" + outputPath;
startInfo.Arguments = args;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
}
}
c# performance video
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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";
Directory.CreateDirectory(pathName);
tempPath = pathName;
}
else
{
string pathName = "C://Documents//tempScreenCaps";
Directory.CreateDirectory(pathName);
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);
File.Delete(file);
}
//Delete the path:
foreach (string dir in dirs)
{
DeletePath(dir);
}
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);
inputImageSequence.Add(name);
fileCount++;
//Dispose of bitmap:
bitmap.Dispose();
}
}
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;
vFWriter.WriteVideoFrame(imageFrame);
imageFrame.Dispose();
}
//Close:
vFWriter.Close();
}
//Delete the screenshots and temporary folder:
DeletePath(tempPath);
//Add audio:
string args = "/c ffmpeg -i "video.mp4" -i "mic.wav" -shortest outPutFile.mp4";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = @"" + outputPath;
startInfo.Arguments = args;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
}
}
c# performance video
New contributor
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";
Directory.CreateDirectory(pathName);
tempPath = pathName;
}
else
{
string pathName = "C://Documents//tempScreenCaps";
Directory.CreateDirectory(pathName);
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);
File.Delete(file);
}
//Delete the path:
foreach (string dir in dirs)
{
DeletePath(dir);
}
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);
inputImageSequence.Add(name);
fileCount++;
//Dispose of bitmap:
bitmap.Dispose();
}
}
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;
vFWriter.WriteVideoFrame(imageFrame);
imageFrame.Dispose();
}
//Close:
vFWriter.Close();
}
//Delete the screenshots and temporary folder:
DeletePath(tempPath);
//Add audio:
string args = "/c ffmpeg -i "video.mp4" -i "mic.wav" -shortest outPutFile.mp4";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = @"" + outputPath;
startInfo.Arguments = args;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
}
}
c# performance video
c# performance video
New contributor
New contributor
edited 16 mins ago
200_success
127k15148412
127k15148412
New contributor
asked 1 hour ago
BenCompSci
463
463
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
BenCompSci is a new contributor. Be nice, and check out our Code of Conduct.
BenCompSci is a new contributor. Be nice, and check out our Code of Conduct.
BenCompSci is a new contributor. Be nice, and check out our Code of Conduct.
BenCompSci is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208975%2fc-screen-recorder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown