Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- 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;
- namespace WorkTracker
- {
- public partial class Form1 : Form
- {
- /* Vars */
- string outputPath = "";
- string path = "";
- int fileCount = 1;
- List<string> inputImageSequence = new List<string>();
- /* Functions */
- void takeScreenshot()
- {
- Rectangle bounds = Screen.FromControl(this).Bounds;
- 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);
- }
- //Create and savescreenshot:
- string name = path + "//screenshot-" + fileCount + ".jpeg";
- bitmap.Save(name, ImageFormat.Jpeg);
- inputImageSequence.Add(name);
- fileCount++;
- //Dispose of bitmap:
- bitmap.Dispose();
- }
- }
- public 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 Form1()
- {
- InitializeComponent();
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- tmrTrack.Start();
- }
- private void tmrTrack_Tick(object sender, EventArgs e)
- {
- takeScreenshot();
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- tmrTrack.Stop();
- //Set bounds of video to screen size:
- Rectangle bounds = Screen.FromControl(this).Bounds;
- int width = bounds.Width;
- int height = bounds.Height;
- var framRate = 5;
- using (var vFWriter = new VideoFileWriter())
- {
- //Create new video file:
- vFWriter.Open(outputPath+"//video.avi", width, height, framRate, VideoCodec.Raw);
- //Make each screenshot into a video frame:
- foreach (var imageLocation in inputImageSequence)
- {
- Bitmap imageFrame = System.Drawing.Image.FromFile(imageLocation) as Bitmap;
- vFWriter.WriteVideoFrame(imageFrame);
- imageFrame.Dispose();
- }
- vFWriter.Close();
- }
- //Delete the screenshots and temporary folder:
- DeletePath(path);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //Create output path:
- FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
- folderBrowser.Description = "Select an Output Folder";
- if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- outputPath = @folderBrowser.SelectedPath;
- }
- else
- {
- MessageBox.Show("Please select an output folder.", "Error");
- Form1_Load(sender, e);
- }
- //Create temporary folder for screenshots:
- if (Directory.Exists("D://"))
- {
- string pathName = "D://tempScreenCaps";
- Directory.CreateDirectory(pathName);
- path = pathName;
- } else
- {
- string pathName = "C://Documents//tempScreenCaps";
- Directory.CreateDirectory(pathName);
- path = pathName;
- }
- }
- private void btnPause_Click(object sender, EventArgs e)
- {
- tmrTrack.Stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement