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.Drawing.Imaging;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using AForge.Video.FFMPEG;
- using AForge.Video;
- using System.Diagnostics;
- namespace ScreenRecord
- {
- public partial class Form1 : Form
- {
- bool rec = false;
- Rectangle screenSize = Screen.PrimaryScreen.Bounds;
- UInt32 frameCount = 0;
- VideoFileWriter writer;
- int width = SystemInformation.VirtualScreen.Width;
- int height = SystemInformation.VirtualScreen.Height;
- AForge.Video.ScreenCaptureStream streamVideo;
- Stopwatch stopWatch = new Stopwatch();
- public Form1()
- {
- InitializeComponent();
- bt_Save.Enabled = false;
- writer = new VideoFileWriter();
- // Codec ComboBox
- cb_VideoCodec.DataSource = Enum.GetValues(typeof(VideoCodec));
- cb_VideoCodec.DropDownStyle = ComboBoxStyle.DropDownList;
- // BitRate 2000kbit/s 2000000 1000000
- cb_BitRate.DataSource = Enum.GetValues(typeof(bitRate));
- cb_BitRate.DropDownStyle = ComboBoxStyle.DropDownList;
- cb_BitRate.SelectedIndex = 3;
- }
- enum bitRate
- {
- _50kbit = 5000,
- _100kbit = 10000,
- _500kbit = 50000,
- _1000kbit = 1000000,
- _2000kbit = 2000000,
- _3000kbit = 3000000
- }
- private void bt_start_Click(object sender, EventArgs e)
- {
- try
- {
- if (tb_SaveFolder.Text.Length < 1)
- {
- FolderBrowserDialog fbd = new FolderBrowserDialog();
- if (fbd.ShowDialog() == DialogResult.OK)
- {
- StartRec(fbd.SelectedPath);
- }
- }
- else
- {
- StartRec(tb_SaveFolder.Text);
- }
- }
- catch (Exception gdfdsgg)
- {
- MessageBox.Show(gdfdsgg.Message);
- }
- }
- private void StartRec(string path)
- {
- if (rec == false)
- {
- bt_start.Enabled = false;
- bt_Save.Enabled = true;
- rec = true;
- frameCount = 0;
- tb_SaveFolder.Text = path;
- string time = DateTime.Now.ToString("d_MMM_yyyy_HH_mm_ssff");
- string compName = Environment.UserName;
- string fullName = path + "\\" + compName.ToUpper() + "_" + time;
- try
- {
- // Save File option
- writer.Open(
- fullName + ".avi",
- width,
- height,
- (int)nud_FPS.Value,
- (VideoCodec)cb_VideoCodec.SelectedValue, (int)(bitRate)cb_BitRate.SelectedValue);
- }
- catch (Exception hgfdhdfs)
- {
- MessageBox.Show(hgfdhdfs.Message);
- }
- // Start main work
- DoJob();
- }
- }
- private void DoJob() //Object stateInfo
- {
- try
- {
- // get entire desktop area size
- Rectangle screenArea = Rectangle.Empty;
- foreach (System.Windows.Forms.Screen screen in
- System.Windows.Forms.Screen.AllScreens)
- {
- screenArea = Rectangle.Union(screenArea, screen.Bounds);
- }
- // create screen capture video source
- streamVideo = new ScreenCaptureStream(screenArea);
- // set NewFrame event handler
- streamVideo.NewFrame += new NewFrameEventHandler(video_NewFrame);
- // start the video source
- streamVideo.Start();
- // stopWatch
- stopWatch.Start();
- }
- catch (Exception gfdgdfhdf)
- {
- MessageBox.Show(gfdgdfhdf.Message);
- }
- }
- private void video_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
- {
- try
- {
- if (rec)
- {
- frameCount++;
- writer.WriteVideoFrame(eventArgs.Frame);
- lb_1.Invoke(new Action(() => { lb_1.Text = "Frames: " + frameCount.ToString(); }));
- lb_stopWatch.Invoke(new Action(() =>
- {
- lb_stopWatch.Text = stopWatch.Elapsed.ToString();
- }));
- }
- else
- {
- stopWatch.Reset();
- Thread.Sleep(500);
- streamVideo.SignalToStop();
- Thread.Sleep(500);
- writer.Close();
- }
- }
- catch (Exception glj)
- {
- MessageBox.Show(glj.Message);
- }
- }
- private void bt_Save_Click(object sender, EventArgs e)
- {
- try
- {
- rec = false;
- bt_start.Enabled = true;
- bt_Save.Enabled = false;
- MessageBox.Show("File saved");
- }
- catch (Exception gfsgsdfgsdf)
- {
- MessageBox.Show(gfsgsdfgsdf.Message);
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- rec = false;
- }
- }
- }
Add Comment
Please, Sign In to add comment