-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
225 lines (190 loc) · 7.51 KB
/
Program.cs
File metadata and controls
225 lines (190 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows.Forms;
namespace screencap
{
static class Program
{
private static MainWindow m_mainWindow;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Idle += Application_Idle;
m_mainWindow = new MainWindow();
m_mainWindow.TopMost = true;
m_mainWindow.AutoScaleMode = AutoScaleMode.Dpi;
m_mainWindow.m_recordButton.Click += OnRecordButtonClicked;
Application.Run(m_mainWindow);
}
[StructLayout(LayoutKind.Sequential)]
private struct Message
{
public IntPtr hWnd;
public int msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public Point p;
}
[return: MarshalAs(UnmanagedType.Bool)]
[SuppressUnmanagedCodeSecurity, DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
// http://stackoverflow.com/questions/5977445/how-to-get-windows-display-settings
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
public enum DeviceCap
{
VERTRES = 10,
DESKTOPVERTRES = 117,
// http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
}
public static float GetScalingFactor()
{
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr desktop = g.GetHdc();
int LogicalScreenHeight = GetDeviceCaps(desktop, (int) DeviceCap.VERTRES);
int PhysicalScreenHeight = GetDeviceCaps(desktop, (int) DeviceCap.DESKTOPVERTRES);
float ScreenScalingFactor = (float) PhysicalScreenHeight / (float) LogicalScreenHeight;
return ScreenScalingFactor; // 1.25 = 125%
}
static List<IEnumerator> m_routines = new List<IEnumerator>();
public static void StartRoutine(IEnumerator routine)
{
m_routines.Add(routine);
}
private static void Application_Idle(object sender, EventArgs e)
{
Message message;
while (!PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
{
Update();
}
}
static bool MoveNextRecursive(IEnumerator routine)
{
if (routine.Current is IEnumerator && MoveNextRecursive((IEnumerator) routine.Current))
{
return true;
}
return routine.MoveNext();
}
public static IEnumerator WaitForSeconds(double seconds)
{
var watch = Stopwatch.StartNew();
while (watch.Elapsed.TotalSeconds < seconds)
yield return null;
}
private static void Update()
{
m_routines.RemoveAll(r => !MoveNextRecursive(r));
}
static bool m_stopRecording = true;
private static void OnRecordButtonClicked(object sender, EventArgs e)
{
m_stopRecording = !m_stopRecording;
((Button) sender).Text = m_stopRecording ? "Record" : "Stop";
if (!m_stopRecording)
StartRoutine(RecordGif());
}
private static IEnumerator RecordGif()
{
var path = RunSaveFileDialog();
if (path == null)
yield break;
var oldBackColor = m_mainWindow.m_statusLabel.BackColor;
yield return DoCountdown();
var scalingFactor = GetScalingFactor();
Func<int, int> Scale = i => (int) (i * scalingFactor);
var fps = GetFPS();
var panel = m_mainWindow.m_imagePanel;
var bmp = CreateBitmap(panel, Scale);
var screenPt = panel.PointToScreen(Point.Empty);
var secsPerFrame = 1.0 / fps;
using (var encoder = new Accord.Video.FFMPEG.VideoFileWriter())
{
encoder.Open(
path, bmp.Size.Width, bmp.Size.Height,
fps, Accord.Video.FFMPEG.VideoCodec.H264, (int) CalculateBitrate(fps, bmp));
var thisFrameWatch = Stopwatch.StartNew();
do
{
CopyScreenToBitmap(bmp, Scale(screenPt.X) + 1, Scale(screenPt.Y) + 1);
encoder.WriteVideoFrame(bmp);
var timeSpentThisFrame = thisFrameWatch.Elapsed.TotalSeconds;
thisFrameWatch.Restart();
yield return WaitForSeconds(secsPerFrame - timeSpentThisFrame);
} while (!m_stopRecording);
encoder.Close();
}
m_mainWindow.m_statusLabel.BackColor = oldBackColor;
//m_mainWindow.m_statusLabel.Text = "";
if (m_mainWindow.m_showExplorerCheckbox.Checked)
{
Process.Start("explorer", $"/select,\"{path}\"");
}
if (m_mainWindow.m_closeAfterCheckbox.Checked)
{
Application.Exit();
}
}
private static double CalculateBitrate(int fps, Bitmap bmp)
{
var bitrate = 2.05433 * Math.Log(0.0000181223 * bmp.Size.Width * bmp.Size.Height); // guess a good bitrate
bitrate *= 1024 * 1024; // mb -> b
bitrate *= 0.721348 * Math.Log(fps / 7.5); // adjust for framerate
bitrate *= 2; // double for good measure :P
return bitrate;
}
private static string RunSaveFileDialog()
{
var dialog = new SaveFileDialog();
dialog.DefaultExt = "mp4";
dialog.Filter = "Videos|*.mp4";
dialog.AddExtension = true;
var dialogResult = dialog.ShowDialog();
var path = dialogResult == DialogResult.OK ? dialog.FileName : null;
return path;
}
private static void CopyScreenToBitmap(Bitmap bmp, int sourceX, int sourceY)
{
using (var graphics = Graphics.FromImage(bmp))
graphics.CopyFromScreen(sourceX, sourceY, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
}
private static int GetFPS()
{
int fps = int.TryParse(m_mainWindow.m_fpsBox.Text, out fps) ? fps : 30;
m_mainWindow.m_fpsBox.Text = fps.ToString();
return fps;
}
private static IEnumerator DoCountdown()
{
for (int i = 3; i > 0; --i)
{
m_mainWindow.m_statusLabel.Text = $"{i}...";
yield return WaitForSeconds(1);
}
m_mainWindow.m_statusLabel.Text = "Recording!";
m_mainWindow.m_statusLabel.BackColor = Color.Red;
}
private static Bitmap CreateBitmap(Panel panel, Func<int, int> Scale)
{
// must be multiples of 2
var size = panel.ClientRectangle.Size;
var width = Scale(size.Width);
width -= width % 2 + 2;
var height = Scale(size.Height);
height -= height % 2 + 2;
return new Bitmap(width, height);
}
}
}