星火微课系统客户端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ScreenRecordingToolbarWindow.xaml.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 + "temp/");
  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. try
  215. {
  216. APP.FFmpeg.SuspendFFmpeg();
  217. }
  218. catch (Exception ex)
  219. {
  220. MessageBox.Show(ex.Message);
  221. }
  222. }
  223. }
  224. /// <summary>
  225. /// 停止录像
  226. /// </summary>
  227. /// <param name="sender"></param>
  228. /// <param name="e"></param>
  229. private void BtnStopRecordingScreen_Click(object sender, RoutedEventArgs e)
  230. {
  231. IsSuspend = true;
  232. txbTime.Text = "00:00";
  233. End();
  234. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
  235. if (gridToolbar.Visibility==Visibility.Visible)
  236. {
  237. gridToolbar.Visibility = Visibility.Hidden;
  238. gridColour.Visibility = Visibility.Hidden;
  239. gridThickness.Visibility = Visibility.Hidden;
  240. APP.W_PracticeWindow.Hide();
  241. }
  242. if (APP.W_XHMicroLessonSystemWindow == null)
  243. {
  244. APP.W_XHMicroLessonSystemWindow = new XHMicroLessonSystemWindow();
  245. }
  246. APP.W_XHMicroLessonSystemWindow.Show();
  247. if (!IsFirstRS)
  248. {
  249. try
  250. {
  251. try
  252. {
  253. APP.FFmpeg.StopFFmpeg(VideoSavePathName);
  254. }
  255. catch (Exception ex)
  256. {
  257. LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
  258. }
  259. IsFirstRS = true;
  260. //生成缩略图
  261. string ThumbnailPath = FileToolsCommon.GetDirectoryName(VideoSavePathName) + "ThumbnailPath/";
  262. FileToolsCommon.CreateDirectory(ThumbnailPath);
  263. //缩略图存储位置
  264. string ThumbnailPathName = ThumbnailPath + FileToolsCommon.GetIOFileNameNoExtension(VideoSavePathName) + ".JPG";
  265. new Thread(new ThreadStart(new Action(() =>
  266. {
  267. while (!FileToolsCommon.IsExistFile(VideoSavePathName))
  268. {
  269. Thread.Sleep(100);
  270. }
  271. FileToolsCommon.DeleteFile(ThumbnailPathName);
  272. //VideoInfo.VideoSize = FileToolsCommon.GetFileSizeByMB(VideoSavePathName).ToString() + " MB";
  273. VideoInfo.RSTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  274. VideoInfo.VideoPath = VideoSavePathName;
  275. VideoInfo.ThumbnailPath = ThumbnailPathName;
  276. APP.FFmpeg.GenerateThumbnails(VideoSavePathName, ThumbnailPathName);
  277. VideoInfo.FileGuid = Guid.NewGuid().ToString();
  278. VideoInfo.IsUpload = false;
  279. VideoInfo.Uploaded = 0;
  280. VideoInfo.Savefolder = APP.UserInfo.Schoolid + "/resource";
  281. APP.VideoList.Add(VideoInfo);
  282. //保存数据
  283. APP.SaveWkData();
  284. }))).Start();
  285. }
  286. catch (Exception ex)
  287. {
  288. LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
  289. }
  290. }
  291. Hide();
  292. }
  293. #endregion
  294. #region 画笔相关
  295. /// <summary>
  296. /// 画笔工具栏关闭事件
  297. /// </summary>
  298. /// <param name="sender"></param>
  299. /// <param name="e"></param>
  300. private void BtnToolbarDown_Click(object sender, RoutedEventArgs e)
  301. {
  302. gridToolbar.Visibility = Visibility.Hidden;
  303. gridColour.Visibility = Visibility.Hidden;
  304. gridThickness.Visibility = Visibility.Hidden;
  305. APP.W_PracticeWindow.Hide();
  306. }
  307. /// <summary>
  308. /// 画笔点击事件
  309. /// </summary>
  310. /// <param name="sender"></param>
  311. /// <param name="e"></param>
  312. private void BtnBrush_Click(object sender, RoutedEventArgs e)
  313. {
  314. string time = GetTimeStamp();
  315. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
  316. if (!Directory.Exists(tempPath))
  317. {
  318. Directory.CreateDirectory(tempPath);
  319. }
  320. string imagePath = Path.Combine(tempPath, time + ".jpg");
  321. ImageHelper.GetScreenshot(new System.Drawing.Rectangle(0, 0, 0, 0), imagePath);
  322. if (gridToolbar.Visibility == Visibility.Visible)
  323. {
  324. gridToolbar.Visibility = Visibility.Hidden;
  325. }
  326. else
  327. {
  328. gridToolbar.Visibility = Visibility.Visible;
  329. try
  330. {
  331. if (APP.W_PracticeWindow == null)
  332. {
  333. APP.W_PracticeWindow = new PracticeWindow();
  334. //APP.W_PracticeWindow.Topmost = true;
  335. APP.W_PracticeWindow.Width = pwidth;
  336. APP.W_PracticeWindow.Height = pHeight;
  337. APP.W_PracticeWindow.Left = 0;
  338. APP.W_PracticeWindow.Top = 0;
  339. //practiceWin.Owner = this;
  340. }
  341. APP.W_PracticeWindow.Initialize(imagePath);
  342. APP.W_PracticeWindow.Show();
  343. }
  344. catch (Exception ex)
  345. {
  346. LogHelper.WriteErrLog("【批注(PracticeWindow)" + ex.Message, ex);
  347. }
  348. }
  349. //imgCanvas.Source = new BitmapImage(new Uri(imagePath));
  350. }
  351. /// <summary>
  352. /// 屏幕宽
  353. /// </summary>
  354. internal double pwidth = SystemParameters.PrimaryScreenWidth;
  355. /// <summary>
  356. /// 屏幕高
  357. /// </summary>
  358. internal double pHeight = SystemParameters.PrimaryScreenHeight;
  359. /// <summary>
  360. /// 获取时间戳
  361. /// </summary>
  362. /// <returns></returns>
  363. public string GetTimeStamp()
  364. {
  365. TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  366. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  367. }
  368. /// <summary>
  369. /// 画笔粗细事件
  370. /// </summary>
  371. /// <param name="sender"></param>
  372. /// <param name="e"></param>
  373. private void BtnThickness_Click(object sender, RoutedEventArgs e)
  374. {
  375. gridThickness.Visibility = Visibility.Visible;
  376. gridColour.Visibility = Visibility.Collapsed;
  377. }
  378. /// <summary>
  379. /// 画笔颜色事件
  380. /// </summary>
  381. /// <param name="sender"></param>
  382. /// <param name="e"></param>
  383. private void BtnColour_Click(object sender, RoutedEventArgs e)
  384. {
  385. gridColour.Visibility = Visibility.Visible;
  386. gridThickness.Visibility = Visibility.Collapsed;
  387. }
  388. /// <summary>
  389. /// 白色
  390. /// </summary>
  391. /// <param name="sender"></param>
  392. /// <param name="e"></param>
  393. private void BtnWhite_Click(object sender, RoutedEventArgs e)
  394. {
  395. //drawingAttributes.Color = Colors.White;
  396. APP.W_PracticeWindow.White();
  397. gridColour.Visibility = Visibility.Collapsed;
  398. gridThickness.Visibility = Visibility.Collapsed;
  399. }
  400. /// <summary>
  401. /// 红色
  402. /// </summary>
  403. /// <param name="sender"></param>
  404. /// <param name="e"></param>
  405. private void BtnRed_Click(object sender, RoutedEventArgs e)
  406. {
  407. //drawingAttributes.Color = Colors.Red;
  408. APP.W_PracticeWindow.Red();
  409. gridColour.Visibility = Visibility.Collapsed;
  410. gridThickness.Visibility = Visibility.Collapsed;
  411. }
  412. /// <summary>
  413. /// 黄色
  414. /// </summary>
  415. /// <param name="sender"></param>
  416. /// <param name="e"></param>
  417. private void BtnYellow_Click(object sender, RoutedEventArgs e)
  418. {
  419. //drawingAttributes.Color = Colors.Gold;
  420. APP.W_PracticeWindow.Yellow();
  421. gridColour.Visibility = Visibility.Collapsed;
  422. gridThickness.Visibility = Visibility.Collapsed;
  423. }
  424. /// <summary>
  425. /// 青色
  426. /// </summary>
  427. /// <param name="sender"></param>
  428. /// <param name="e"></param>
  429. private void BtnCyanBlue_Click(object sender, RoutedEventArgs e)
  430. {
  431. //drawingAttributes.Color = Colors.LimeGreen;
  432. APP.W_PracticeWindow.CyanBlue();
  433. gridColour.Visibility = Visibility.Collapsed;
  434. gridThickness.Visibility = Visibility.Collapsed;
  435. }
  436. /// <summary>
  437. /// 灰色
  438. /// </summary>
  439. /// <param name="sender"></param>
  440. /// <param name="e"></param>
  441. private void BtnGray_Click(object sender, RoutedEventArgs e)
  442. {
  443. //drawingAttributes.Color = Colors.Gray;
  444. APP.W_PracticeWindow.Gray();
  445. gridColour.Visibility = Visibility.Collapsed;
  446. gridThickness.Visibility = Visibility.Collapsed;
  447. }
  448. /// <summary>
  449. /// 蓝色
  450. /// </summary>
  451. /// <param name="sender"></param>
  452. /// <param name="e"></param>
  453. private void BtnBlue_Click(object sender, RoutedEventArgs e)
  454. {
  455. //drawingAttributes.Color = Colors.DeepSkyBlue;
  456. APP.W_PracticeWindow.Blue();
  457. gridColour.Visibility = Visibility.Collapsed;
  458. gridThickness.Visibility = Visibility.Collapsed;
  459. }
  460. /// <summary>
  461. /// 画笔 细
  462. /// </summary>
  463. /// <param name="sender"></param>
  464. /// <param name="e"></param>
  465. private void BtnFine_Click(object sender, RoutedEventArgs e)
  466. {
  467. APP.W_PracticeWindow.Fine();
  468. gridColour.Visibility = Visibility.Collapsed;
  469. gridThickness.Visibility = Visibility.Collapsed;
  470. }
  471. /// <summary>
  472. /// 画笔 中
  473. /// </summary>
  474. /// <param name="sender"></param>
  475. /// <param name="e"></param>
  476. private void BtnIn_Click(object sender, RoutedEventArgs e)
  477. {
  478. APP.W_PracticeWindow.In();
  479. gridColour.Visibility = Visibility.Collapsed;
  480. gridThickness.Visibility = Visibility.Collapsed;
  481. }
  482. /// <summary>
  483. /// 画笔粗
  484. /// </summary>
  485. /// <param name="sender"></param>
  486. /// <param name="e"></param>
  487. private void BtnCrude_Click(object sender, RoutedEventArgs e)
  488. {
  489. APP.W_PracticeWindow.Crude();
  490. gridColour.Visibility = Visibility.Collapsed;
  491. gridThickness.Visibility = Visibility.Collapsed;
  492. }
  493. /// <summary>
  494. /// 橡皮
  495. /// </summary>
  496. /// <param name="sender"></param>
  497. /// <param name="e"></param>
  498. private void BtnEraser_Click(object sender, RoutedEventArgs e)
  499. {
  500. APP.W_PracticeWindow.Eraser();
  501. }
  502. /// <summary>
  503. /// 🖊
  504. /// </summary>
  505. /// <param name="sender"></param>
  506. /// <param name="e"></param>
  507. private void BtnPen_Click(object sender, RoutedEventArgs e)
  508. {
  509. APP.W_PracticeWindow.Pen();
  510. }
  511. /// <summary>
  512. /// ⚪
  513. /// </summary>
  514. /// <param name="sender"></param>
  515. /// <param name="e"></param>
  516. private void BtnRound_Click(object sender, RoutedEventArgs e)
  517. {
  518. APP.W_PracticeWindow.Round();
  519. }
  520. /// <summary>
  521. /// 矩形
  522. /// </summary>
  523. /// <param name="sender"></param>
  524. /// <param name="e"></param>
  525. private void BtnRectangle_Click(object sender, RoutedEventArgs e)
  526. {
  527. APP.W_PracticeWindow.Rectangle();
  528. }
  529. #endregion
  530. #endregion
  531. }
  532. }