星火微课系统客户端
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ScreenRecordingToolbarWindow.xaml.cs 21KB

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