星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ScreenRecordingToolbarWindow.xaml.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using Common.system;
  2. using System;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Windows.Ink;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. using System.Windows.Threading;
  11. using XHWK.Model;
  12. namespace XHWK.WKTool
  13. {
  14. /// <summary>
  15. /// 录屏工具栏
  16. /// </summary>
  17. public partial class ScreenRecordingToolbarWindow : Window
  18. {
  19. #region 初始变量
  20. /// <summary>
  21. /// 视频存储路径
  22. /// </summary>
  23. string RecordingPath;
  24. /// <summary>
  25. /// 视频保存名称
  26. /// </summary>
  27. string VideoSavePathName;
  28. /// <summary>
  29. /// 是否首次录屏
  30. /// </summary>
  31. bool IsFirstRS = true;
  32. /// <summary>
  33. /// 是否暂停
  34. /// </summary>
  35. bool IsSuspend = true;
  36. /// <summary>
  37. /// 视频信息
  38. /// </summary>
  39. Model_Video VideoInfo = null;
  40. //声明一个 DrawingAttributes 类型的变量
  41. DrawingAttributes drawingAttributes;
  42. private DispatcherTimer t = null;
  43. /// <summary>
  44. /// 计时器状态
  45. /// </summary>
  46. private enum State
  47. {
  48. Start,
  49. Pause,
  50. End
  51. }
  52. /// <summary>
  53. /// 状态
  54. /// </summary>
  55. private State _state = State.End;
  56. /// <summary>
  57. /// 计时用
  58. /// </summary>
  59. private TimeSpan _timeSpan = new TimeSpan(0, 0, 0, 0, 0);
  60. #endregion
  61. #region 初始化
  62. /// <summary>
  63. /// 录屏工具栏
  64. /// </summary>
  65. public ScreenRecordingToolbarWindow()
  66. {
  67. InitializeComponent();
  68. }
  69. /// <summary>
  70. /// 初始化
  71. /// </summary>
  72. public void Initialize()
  73. {
  74. //创建 DrawingAttributes 类的一个实例
  75. drawingAttributes = new DrawingAttributes();
  76. //将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用
  77. //InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型
  78. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  79. drawingAttributes.FitToCurve = true;
  80. //隐藏画笔工具栏
  81. //BtnToolbarDown_Click(null, null);
  82. gridToolbar.Visibility = Visibility.Hidden;
  83. gridColour.Visibility = Visibility.Hidden;
  84. gridThickness.Visibility = Visibility.Hidden;
  85. if (t == null)
  86. {
  87. t = new DispatcherTimer();
  88. t.Tick += OnTimer;
  89. t.Interval = new TimeSpan(0, 0, 0, 1);
  90. t.IsEnabled = true;
  91. t.Start();
  92. }
  93. t.Interval = new TimeSpan(0, 0, 0, 1);
  94. Stack();
  95. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
  96. BtnRecordingScreen_Click(null, null);
  97. }
  98. #endregion
  99. #region 事件
  100. /// <summary>
  101. /// 时钟回调
  102. /// </summary>
  103. /// <param name="sender"></param>
  104. /// <param name="e"></param>
  105. private void OnTimer(object sender, EventArgs e)
  106. {
  107. switch (_state)
  108. {
  109. case State.Start:
  110. {
  111. _timeSpan += new TimeSpan(0, 0, 0, 1);
  112. }
  113. break;
  114. case State.Pause:
  115. {
  116. }
  117. break;
  118. case State.End:
  119. {
  120. _timeSpan = new TimeSpan();
  121. //_timeSpan = new TimeSpan(0, 23, 12, 45, 54);
  122. }
  123. break;
  124. }
  125. string time = string.Format("{0:D2}:{1:D2}",/* _timeSpan.Hours,*/ _timeSpan.Minutes, _timeSpan.Seconds);
  126. //char[] times = time.ToCharArray();
  127. txbTime.Text = time;
  128. }
  129. /// <summary>
  130. /// 开始
  131. /// </summary>
  132. /// <param name="sender"></param>
  133. /// <param name="e"></param>
  134. private void Stack()
  135. {
  136. _state = State.Start;
  137. }
  138. /// <summary>
  139. /// 暂停
  140. /// </summary>
  141. private void TimeOut()
  142. {
  143. _state = State.Pause;
  144. }
  145. /// <summary>
  146. /// 结束
  147. /// </summary>
  148. /// <param name="sender"></param>
  149. /// <param name="e"></param>
  150. private void End()
  151. {
  152. _state = State.End;
  153. }
  154. #region 录制
  155. /// <summary>
  156. /// 开始或暂停录制
  157. /// </summary>
  158. /// <param name="sender"></param>
  159. /// <param name="e"></param>
  160. private void BtnRecordingScreen_Click(object sender, RoutedEventArgs e)
  161. {
  162. if (IsSuspend)
  163. {
  164. Stack();
  165. if (IsFirstRS)
  166. {
  167. VideoInfo = new Model_Video();
  168. VideoInfo.VideoType = (Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType"));
  169. VideoInfo.WkType = Enum_WKVidetype.RecordingScreen;
  170. RecordingPath = APP.WKData.WkPath;
  171. //FileToolsCommon.DeleteDirectory(APP.WKData.WkPath + "temp/");
  172. FileToolsCommon.CreateDirectory(RecordingPath);
  173. VideoSavePathName = RecordingPath + APP.WKData.WkName + "_录屏." + ((Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType"))).ToString();
  174. if (FileToolsCommon.IsExistFile(VideoSavePathName))
  175. {
  176. MessageBoxResult dr = System.Windows.MessageBox.Show("已存在录屏,是否覆盖?", "提示", MessageBoxButton.OKCancel);
  177. if (dr == MessageBoxResult.OK)
  178. {
  179. try
  180. {
  181. FileToolsCommon.DeleteFile(VideoSavePathName);
  182. APP.VideoList.RemoveAll(x => x.VidePath == VideoSavePathName);
  183. }
  184. catch (Exception ex)
  185. {
  186. LogHelper.WriteErrLog("【录屏】(BtnRecordingScreen_Click)无法移除视频," + ex.Message, ex);
  187. }
  188. }
  189. else
  190. {
  191. return;
  192. }
  193. }
  194. IsFirstRS = false;
  195. }
  196. IsSuspend = false;
  197. //#region 录像倒计时
  198. //if (APP.W_CountdownWindow == null)
  199. //{
  200. // APP.W_CountdownWindow = new CountdownWindow();
  201. // //APP.W_CountdownWindow.Topmost = true;
  202. //}
  203. //else
  204. //{
  205. // APP.W_CountdownWindow.Initialize();
  206. // //APP.W_CountdownWindow.Topmost = true;
  207. //}
  208. //APP.W_CountdownWindow.Show();
  209. //#endregion
  210. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
  211. try
  212. {
  213. APP.FFmpeg.StartRecordingVideo(VideoSavePathName);
  214. }
  215. catch (Exception ex)
  216. {
  217. MessageBox.Show(ex.Message);
  218. }
  219. }
  220. else
  221. {
  222. TimeOut();
  223. IsSuspend = true;
  224. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/microLessonSystem_14.png"));
  225. try
  226. {
  227. APP.FFmpeg.SuspendFFmpeg();
  228. }
  229. catch (Exception ex)
  230. {
  231. MessageBox.Show(ex.Message);
  232. }
  233. }
  234. }
  235. /// <summary>
  236. /// 停止录像
  237. /// </summary>
  238. /// <param name="sender"></param>
  239. /// <param name="e"></param>
  240. private void BtnStopRecordingScreen_Click(object sender, RoutedEventArgs e)
  241. {
  242. IsSuspend = true;
  243. txbTime.Text = "00:00";
  244. End();
  245. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
  246. if (gridToolbar.Visibility==Visibility.Visible)
  247. {
  248. gridToolbar.Visibility = Visibility.Hidden;
  249. gridColour.Visibility = Visibility.Hidden;
  250. gridThickness.Visibility = Visibility.Hidden;
  251. APP.W_PracticeWindow.Hide();
  252. }
  253. if (APP.W_XHMicroLessonSystemWindow == null)
  254. {
  255. APP.W_XHMicroLessonSystemWindow = new XHMicroLessonSystemWindow();
  256. }
  257. APP.W_XHMicroLessonSystemWindow.Show();
  258. if (!IsFirstRS)
  259. {
  260. try
  261. {
  262. try
  263. {
  264. APP.FFmpeg.StopFFmpeg(VideoSavePathName);
  265. }
  266. catch (Exception ex)
  267. {
  268. LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
  269. }
  270. IsFirstRS = true;
  271. //生成缩略图
  272. string ThumbnailPath = FileToolsCommon.GetDirectoryName(VideoSavePathName) + "ThumbnailPath/";
  273. FileToolsCommon.CreateDirectory(ThumbnailPath);
  274. //缩略图存储位置
  275. string ThumbnailPathName = ThumbnailPath + FileToolsCommon.GetIOFileNameNoExtension(VideoSavePathName) + ".JPG";
  276. new Thread(new ThreadStart(new Action(() =>
  277. {
  278. while (!FileToolsCommon.IsExistFile(VideoSavePathName))
  279. {
  280. Thread.Sleep(100);
  281. }
  282. FileToolsCommon.DeleteFile(ThumbnailPathName);
  283. VideoInfo.VideoSize = FileToolsCommon.GetFileSizeByMB(VideoSavePathName).ToString() + " MB";
  284. VideoInfo.RSTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  285. VideoInfo.VidePath = VideoSavePathName;
  286. VideoInfo.ThumbnailPath = ThumbnailPathName;
  287. APP.FFmpeg.GenerateThumbnails(VideoSavePathName, ThumbnailPathName);
  288. APP.VideoList.Add(VideoInfo);
  289. //保存数据
  290. APP.SaveWkData();
  291. }))).Start();
  292. }
  293. catch (Exception ex)
  294. {
  295. LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
  296. }
  297. }
  298. Hide();
  299. }
  300. #endregion
  301. #region 画笔相关
  302. /// <summary>
  303. /// 画笔工具栏关闭事件
  304. /// </summary>
  305. /// <param name="sender"></param>
  306. /// <param name="e"></param>
  307. private void BtnToolbarDown_Click(object sender, RoutedEventArgs e)
  308. {
  309. gridToolbar.Visibility = Visibility.Hidden;
  310. gridColour.Visibility = Visibility.Hidden;
  311. gridThickness.Visibility = Visibility.Hidden;
  312. APP.W_PracticeWindow.Hide();
  313. }
  314. /// <summary>
  315. /// 画笔点击事件
  316. /// </summary>
  317. /// <param name="sender"></param>
  318. /// <param name="e"></param>
  319. private void BtnBrush_Click(object sender, RoutedEventArgs e)
  320. {
  321. string time = GetTimeStamp();
  322. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
  323. if (!Directory.Exists(tempPath))
  324. {
  325. Directory.CreateDirectory(tempPath);
  326. }
  327. string imagePath = Path.Combine(tempPath, time + ".jpg");
  328. ImageHelper.GetScreenshot(new System.Drawing.Rectangle(0, 0, 0, 0), imagePath);
  329. if (gridToolbar.Visibility == Visibility.Visible)
  330. {
  331. gridToolbar.Visibility = Visibility.Hidden;
  332. }
  333. else
  334. {
  335. gridToolbar.Visibility = Visibility.Visible;
  336. try
  337. {
  338. if (APP.W_PracticeWindow == null)
  339. {
  340. APP.W_PracticeWindow = new PracticeWindow();
  341. //APP.W_PracticeWindow.Topmost = true;
  342. APP.W_PracticeWindow.Width = pwidth;
  343. APP.W_PracticeWindow.Height = pHeight;
  344. APP.W_PracticeWindow.Left = 0;
  345. APP.W_PracticeWindow.Top = 0;
  346. //practiceWin.Owner = this;
  347. }
  348. APP.W_PracticeWindow.Initialize(imagePath);
  349. APP.W_PracticeWindow.Show();
  350. }
  351. catch (Exception ex)
  352. {
  353. LogHelper.WriteErrLog("【批注(PracticeWindow)" + ex.Message, ex);
  354. }
  355. }
  356. //imgCanvas.Source = new BitmapImage(new Uri(imagePath));
  357. }
  358. /// <summary>
  359. /// 屏幕宽
  360. /// </summary>
  361. internal double pwidth = SystemParameters.PrimaryScreenWidth;
  362. /// <summary>
  363. /// 屏幕高
  364. /// </summary>
  365. internal double pHeight = SystemParameters.PrimaryScreenHeight;
  366. /// <summary>
  367. /// 获取时间戳
  368. /// </summary>
  369. /// <returns></returns>
  370. public string GetTimeStamp()
  371. {
  372. TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  373. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  374. }
  375. /// <summary>
  376. /// 画笔粗细事件
  377. /// </summary>
  378. /// <param name="sender"></param>
  379. /// <param name="e"></param>
  380. private void BtnThickness_Click(object sender, RoutedEventArgs e)
  381. {
  382. gridThickness.Visibility = Visibility.Visible;
  383. gridColour.Visibility = Visibility.Collapsed;
  384. }
  385. /// <summary>
  386. /// 画笔颜色事件
  387. /// </summary>
  388. /// <param name="sender"></param>
  389. /// <param name="e"></param>
  390. private void BtnColour_Click(object sender, RoutedEventArgs e)
  391. {
  392. gridColour.Visibility = Visibility.Visible;
  393. gridThickness.Visibility = Visibility.Collapsed;
  394. }
  395. /// <summary>
  396. /// 白色
  397. /// </summary>
  398. /// <param name="sender"></param>
  399. /// <param name="e"></param>
  400. private void BtnWhite_Click(object sender, RoutedEventArgs e)
  401. {
  402. //drawingAttributes.Color = Colors.White;
  403. APP.W_PracticeWindow.White();
  404. gridColour.Visibility = Visibility.Collapsed;
  405. gridThickness.Visibility = Visibility.Collapsed;
  406. }
  407. /// <summary>
  408. /// 红色
  409. /// </summary>
  410. /// <param name="sender"></param>
  411. /// <param name="e"></param>
  412. private void BtnRed_Click(object sender, RoutedEventArgs e)
  413. {
  414. //drawingAttributes.Color = Colors.Red;
  415. APP.W_PracticeWindow.Red();
  416. gridColour.Visibility = Visibility.Collapsed;
  417. gridThickness.Visibility = Visibility.Collapsed;
  418. }
  419. /// <summary>
  420. /// 黄色
  421. /// </summary>
  422. /// <param name="sender"></param>
  423. /// <param name="e"></param>
  424. private void BtnYellow_Click(object sender, RoutedEventArgs e)
  425. {
  426. //drawingAttributes.Color = Colors.Gold;
  427. APP.W_PracticeWindow.Yellow();
  428. gridColour.Visibility = Visibility.Collapsed;
  429. gridThickness.Visibility = Visibility.Collapsed;
  430. }
  431. /// <summary>
  432. /// 青色
  433. /// </summary>
  434. /// <param name="sender"></param>
  435. /// <param name="e"></param>
  436. private void BtnCyanBlue_Click(object sender, RoutedEventArgs e)
  437. {
  438. //drawingAttributes.Color = Colors.LimeGreen;
  439. APP.W_PracticeWindow.CyanBlue();
  440. gridColour.Visibility = Visibility.Collapsed;
  441. gridThickness.Visibility = Visibility.Collapsed;
  442. }
  443. /// <summary>
  444. /// 灰色
  445. /// </summary>
  446. /// <param name="sender"></param>
  447. /// <param name="e"></param>
  448. private void BtnGray_Click(object sender, RoutedEventArgs e)
  449. {
  450. //drawingAttributes.Color = Colors.Gray;
  451. APP.W_PracticeWindow.Gray();
  452. gridColour.Visibility = Visibility.Collapsed;
  453. gridThickness.Visibility = Visibility.Collapsed;
  454. }
  455. /// <summary>
  456. /// 蓝色
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="e"></param>
  460. private void BtnBlue_Click(object sender, RoutedEventArgs e)
  461. {
  462. //drawingAttributes.Color = Colors.DeepSkyBlue;
  463. APP.W_PracticeWindow.Blue();
  464. gridColour.Visibility = Visibility.Collapsed;
  465. gridThickness.Visibility = Visibility.Collapsed;
  466. }
  467. /// <summary>
  468. /// 画笔 细
  469. /// </summary>
  470. /// <param name="sender"></param>
  471. /// <param name="e"></param>
  472. private void BtnFine_Click(object sender, RoutedEventArgs e)
  473. {
  474. APP.W_PracticeWindow.Fine();
  475. gridColour.Visibility = Visibility.Collapsed;
  476. gridThickness.Visibility = Visibility.Collapsed;
  477. }
  478. /// <summary>
  479. /// 画笔 中
  480. /// </summary>
  481. /// <param name="sender"></param>
  482. /// <param name="e"></param>
  483. private void BtnIn_Click(object sender, RoutedEventArgs e)
  484. {
  485. APP.W_PracticeWindow.In();
  486. gridColour.Visibility = Visibility.Collapsed;
  487. gridThickness.Visibility = Visibility.Collapsed;
  488. }
  489. /// <summary>
  490. /// 画笔粗
  491. /// </summary>
  492. /// <param name="sender"></param>
  493. /// <param name="e"></param>
  494. private void BtnCrude_Click(object sender, RoutedEventArgs e)
  495. {
  496. APP.W_PracticeWindow.Crude();
  497. gridColour.Visibility = Visibility.Collapsed;
  498. gridThickness.Visibility = Visibility.Collapsed;
  499. }
  500. /// <summary>
  501. /// 橡皮
  502. /// </summary>
  503. /// <param name="sender"></param>
  504. /// <param name="e"></param>
  505. private void BtnEraser_Click(object sender, RoutedEventArgs e)
  506. {
  507. APP.W_PracticeWindow.Eraser();
  508. }
  509. /// <summary>
  510. /// 🖊
  511. /// </summary>
  512. /// <param name="sender"></param>
  513. /// <param name="e"></param>
  514. private void BtnPen_Click(object sender, RoutedEventArgs e)
  515. {
  516. APP.W_PracticeWindow.Pen();
  517. }
  518. /// <summary>
  519. /// ⚪
  520. /// </summary>
  521. /// <param name="sender"></param>
  522. /// <param name="e"></param>
  523. private void BtnRound_Click(object sender, RoutedEventArgs e)
  524. {
  525. APP.W_PracticeWindow.Round();
  526. }
  527. /// <summary>
  528. /// 矩形
  529. /// </summary>
  530. /// <param name="sender"></param>
  531. /// <param name="e"></param>
  532. private void BtnRectangle_Click(object sender, RoutedEventArgs e)
  533. {
  534. APP.W_PracticeWindow.Rectangle();
  535. }
  536. #endregion
  537. #endregion
  538. }
  539. }