SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | using System.Collections.Generic; | |
3 | using System.ComponentModel; | |
4 | using System.Data; | |
5 | using System.Drawing; | |
6 | using System.Drawing.Imaging; | |
7 | using System.Linq; | |
8 | using System.Runtime.InteropServices; | |
9 | using System.Text; | |
10 | using System.Threading; | |
11 | using System.Threading.Tasks; | |
12 | using System.Windows.Forms; | |
13 | ||
14 | using AForge.Video.FFMPEG; | |
15 | using AForge.Video; | |
16 | using System.Diagnostics; | |
17 | ||
18 | namespace ScreenRecord | |
19 | { | |
20 | public partial class Form1 : Form | |
21 | { | |
22 | bool rec = false; | |
23 | ||
24 | Rectangle screenSize = Screen.PrimaryScreen.Bounds; | |
25 | ||
26 | UInt32 frameCount = 0; | |
27 | ||
28 | VideoFileWriter writer; | |
29 | ||
30 | int width = SystemInformation.VirtualScreen.Width; | |
31 | int height = SystemInformation.VirtualScreen.Height; | |
32 | ||
33 | AForge.Video.ScreenCaptureStream streamVideo; | |
34 | ||
35 | Stopwatch stopWatch = new Stopwatch(); | |
36 | ||
37 | public Form1() | |
38 | { | |
39 | InitializeComponent(); | |
40 | bt_Save.Enabled = false; | |
41 | writer = new VideoFileWriter(); | |
42 | ||
43 | // Codec ComboBox | |
44 | cb_VideoCodec.DataSource = Enum.GetValues(typeof(VideoCodec)); | |
45 | cb_VideoCodec.DropDownStyle = ComboBoxStyle.DropDownList; | |
46 | ||
47 | // BitRate 2000kbit/s 2000000 1000000 | |
48 | cb_BitRate.DataSource = Enum.GetValues(typeof(bitRate)); | |
49 | cb_BitRate.DropDownStyle = ComboBoxStyle.DropDownList; | |
50 | cb_BitRate.SelectedIndex = 3; | |
51 | } | |
52 | ||
53 | enum bitRate | |
54 | { | |
55 | _50kbit = 5000, | |
56 | _100kbit = 10000, | |
57 | _500kbit = 50000, | |
58 | _1000kbit = 1000000, | |
59 | _2000kbit = 2000000, | |
60 | _3000kbit = 3000000 | |
61 | } | |
62 | ||
63 | private void bt_start_Click(object sender, EventArgs e) | |
64 | { | |
65 | try | |
66 | { | |
67 | if (tb_SaveFolder.Text.Length < 1) | |
68 | { | |
69 | FolderBrowserDialog fbd = new FolderBrowserDialog(); | |
70 | if (fbd.ShowDialog() == DialogResult.OK) | |
71 | { | |
72 | StartRec(fbd.SelectedPath); | |
73 | } | |
74 | } | |
75 | else | |
76 | { | |
77 | StartRec(tb_SaveFolder.Text); | |
78 | } | |
79 | } | |
80 | catch (Exception gdfdsgg) | |
81 | { | |
82 | MessageBox.Show(gdfdsgg.Message); | |
83 | } | |
84 | } | |
85 | ||
86 | private void StartRec(string path) | |
87 | { | |
88 | if (rec == false) | |
89 | { | |
90 | bt_start.Enabled = false; | |
91 | bt_Save.Enabled = true; | |
92 | rec = true; | |
93 | ||
94 | frameCount = 0; | |
95 | ||
96 | tb_SaveFolder.Text = path; | |
97 | string time = DateTime.Now.ToString("d_MMM_yyyy_HH_mm_ssff"); | |
98 | string compName = Environment.UserName; | |
99 | string fullName = path + "\\" + compName.ToUpper() + "_" + time; | |
100 | ||
101 | try | |
102 | { | |
103 | // Save File option | |
104 | writer.Open( | |
105 | fullName + ".avi", | |
106 | width, | |
107 | height, | |
108 | (int)nud_FPS.Value, | |
109 | (VideoCodec)cb_VideoCodec.SelectedValue, (int)(bitRate)cb_BitRate.SelectedValue); | |
110 | } | |
111 | catch (Exception hgfdhdfs) | |
112 | { | |
113 | MessageBox.Show(hgfdhdfs.Message); | |
114 | } | |
115 | // Start main work | |
116 | DoJob(); | |
117 | } | |
118 | } | |
119 | ||
120 | private void DoJob() //Object stateInfo | |
121 | { | |
122 | try | |
123 | { | |
124 | // get entire desktop area size | |
125 | Rectangle screenArea = Rectangle.Empty; | |
126 | foreach (System.Windows.Forms.Screen screen in | |
127 | System.Windows.Forms.Screen.AllScreens) | |
128 | { | |
129 | screenArea = Rectangle.Union(screenArea, screen.Bounds); | |
130 | } | |
131 | ||
132 | // create screen capture video source | |
133 | streamVideo = new ScreenCaptureStream(screenArea); | |
134 | ||
135 | // set NewFrame event handler | |
136 | streamVideo.NewFrame += new NewFrameEventHandler(video_NewFrame); | |
137 | ||
138 | // start the video source | |
139 | streamVideo.Start(); | |
140 | ||
141 | // stopWatch | |
142 | stopWatch.Start(); | |
143 | } | |
144 | catch (Exception gfdgdfhdf) | |
145 | { | |
146 | MessageBox.Show(gfdgdfhdf.Message); | |
147 | } | |
148 | } | |
149 | ||
150 | private void video_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) | |
151 | { | |
152 | try | |
153 | { | |
154 | if (rec) | |
155 | { | |
156 | frameCount++; | |
157 | writer.WriteVideoFrame(eventArgs.Frame); | |
158 | lb_1.Invoke(new Action(() => { lb_1.Text = "Frames: " + frameCount.ToString(); })); | |
159 | lb_stopWatch.Invoke(new Action(() => | |
160 | { | |
161 | lb_stopWatch.Text = stopWatch.Elapsed.ToString(); | |
162 | })); | |
163 | } | |
164 | else | |
165 | { | |
166 | stopWatch.Reset(); | |
167 | Thread.Sleep(500); | |
168 | streamVideo.SignalToStop(); | |
169 | Thread.Sleep(500); | |
170 | writer.Close(); | |
171 | } | |
172 | } | |
173 | catch (Exception glj) | |
174 | { | |
175 | MessageBox.Show(glj.Message); | |
176 | } | |
177 | } | |
178 | ||
179 | private void bt_Save_Click(object sender, EventArgs e) | |
180 | { | |
181 | try | |
182 | { | |
183 | rec = false; | |
184 | bt_start.Enabled = true; | |
185 | bt_Save.Enabled = false; | |
186 | ||
187 | MessageBox.Show("File saved"); | |
188 | } | |
189 | catch (Exception gfsgsdfgsdf) | |
190 | { | |
191 | MessageBox.Show(gfsgsdfgsdf.Message); | |
192 | } | |
193 | } | |
194 | ||
195 | private void Form1_FormClosing(object sender, FormClosingEventArgs e) | |
196 | { | |
197 | rec = false; | |
198 | } | |
199 | } | |
200 | } |