Advertisement
TermSpar

Work Tracker

Nov 25th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Drawing.Imaging;
  11. using Accord.Video.FFMPEG;
  12. using System.IO;
  13.  
  14. namespace WorkTracker
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         /* Vars */
  19.  
  20.         string outputPath = "";
  21.         string path = "";
  22.         int fileCount = 1;
  23.         List<string> inputImageSequence = new List<string>();
  24.  
  25.         /* Functions */
  26.  
  27.         void takeScreenshot()
  28.         {
  29.             Rectangle bounds = Screen.FromControl(this).Bounds;
  30.             using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
  31.             {
  32.                 using (Graphics g = Graphics.FromImage(bitmap))
  33.                 {
  34.                     //Add screen to bitmap:
  35.                     g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
  36.                 }
  37.                 //Create and savescreenshot:
  38.                 string name = path + "//screenshot-" + fileCount + ".jpeg";
  39.                 bitmap.Save(name, ImageFormat.Jpeg);
  40.                 inputImageSequence.Add(name);
  41.                 fileCount++;
  42.  
  43.                 //Dispose of bitmap:
  44.                 bitmap.Dispose();
  45.             }
  46.         }
  47.  
  48.         public static void DeletePath(string target_dir)
  49.         {
  50.             string[] files = Directory.GetFiles(target_dir);
  51.             string[] dirs = Directory.GetDirectories(target_dir);
  52.  
  53.             //Delete each screenshot:
  54.             foreach (string file in files)
  55.             {
  56.                 File.SetAttributes(file, FileAttributes.Normal);
  57.                 File.Delete(file);
  58.             }
  59.  
  60.             //Delete the path:
  61.             foreach (string dir in dirs)
  62.             {
  63.                 DeletePath(dir);
  64.             }
  65.  
  66.             Directory.Delete(target_dir, false);
  67.         }
  68.  
  69.         public Form1()
  70.         {
  71.             InitializeComponent();
  72.         }
  73.  
  74.         private void btnStart_Click(object sender, EventArgs e)
  75.         {
  76.             tmrTrack.Start();
  77.         }
  78.  
  79.         private void tmrTrack_Tick(object sender, EventArgs e)
  80.         {
  81.             takeScreenshot();
  82.         }
  83.  
  84.         private void btnStop_Click(object sender, EventArgs e)
  85.         {
  86.             tmrTrack.Stop();
  87.  
  88.             //Set bounds of video to screen size:
  89.             Rectangle bounds = Screen.FromControl(this).Bounds;
  90.             int width = bounds.Width;
  91.             int height = bounds.Height;
  92.  
  93.             var framRate = 5;
  94.  
  95.             using (var vFWriter = new VideoFileWriter())
  96.             {
  97.                 //Create new video file:
  98.                 vFWriter.Open(outputPath+"//video.avi", width, height, framRate, VideoCodec.Raw);
  99.  
  100.                 //Make each screenshot into a video frame:
  101.                 foreach (var imageLocation in inputImageSequence)
  102.                 {
  103.                     Bitmap imageFrame = System.Drawing.Image.FromFile(imageLocation) as Bitmap;
  104.                     vFWriter.WriteVideoFrame(imageFrame);
  105.                     imageFrame.Dispose();
  106.                 }
  107.                 vFWriter.Close();
  108.             }
  109.             //Delete the screenshots and temporary folder:
  110.             DeletePath(path);
  111.         }
  112.  
  113.         private void Form1_Load(object sender, EventArgs e)
  114.         {
  115.             //Create output path:
  116.             FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
  117.             folderBrowser.Description = "Select an Output Folder";
  118.  
  119.             if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  120.             {
  121.                 outputPath = @folderBrowser.SelectedPath;
  122.             }
  123.             else
  124.             {
  125.                 MessageBox.Show("Please select an output folder.", "Error");
  126.                 Form1_Load(sender, e);
  127.             }
  128.  
  129.             //Create temporary folder for screenshots:
  130.             if (Directory.Exists("D://"))
  131.             {
  132.                 string pathName = "D://tempScreenCaps";
  133.                 Directory.CreateDirectory(pathName);
  134.                 path = pathName;
  135.             } else
  136.             {
  137.                 string pathName = "C://Documents//tempScreenCaps";
  138.                 Directory.CreateDirectory(pathName);
  139.                 path = pathName;
  140.             }
  141.         }
  142.  
  143.         private void btnPause_Click(object sender, EventArgs e)
  144.         {
  145.             tmrTrack.Stop();
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement