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();
}

}
}
}









share|improve this question









New contributor




BenCompSci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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();
    }

    }
    }
    }









    share|improve this question









    New contributor




    BenCompSci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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();
      }

      }
      }
      }









      share|improve this question









      New contributor




      BenCompSci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      BenCompSci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      BenCompSci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 16 mins ago









      200_success

      127k15148412




      127k15148412






      New contributor




      BenCompSci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      BenCompSci

      463




      463




      New contributor




      BenCompSci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      BenCompSci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      BenCompSci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          BenCompSci is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          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






























          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.










          draft saved

          draft discarded


















          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin