星火微课系统客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

JieTuWindow.xaml.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. using ComeCapture.Controls;
  2. using ComeCapture.Helpers;
  3. using ComeCapture.Models;
  4. using Common.system;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using XHWK.WKTool;
  16. using static XHWK.WKTool.XHMicroLessonSystemWindow;
  17. namespace ComeCapture
  18. {
  19. /// <summary>
  20. /// 截图 MainWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class JieTuWindow : Window
  23. {
  24. public static double ScreenWidth = SystemParameters.PrimaryScreenWidth;
  25. public static double ScreenHeight = SystemParameters.PrimaryScreenHeight;
  26. public static double ScreenScale = 1;
  27. public static int MinSize = 10;
  28. internal double pwidth = SystemParameters.PrimaryScreenWidth;
  29. internal double pHeight = SystemParameters.PrimaryScreenHeight;
  30. //画图注册名称集合
  31. public List<NameAndLimit> list = new List<NameAndLimit>();
  32. //画图注册名称
  33. public int num = 1;
  34. //是否截图开始
  35. private bool _IsMouseDown = false;
  36. //是否截图完毕
  37. private bool _IsCapture = false;
  38. private double _X0 = 0;
  39. private double _Y0 = 0;
  40. //private IntPtr BitmapPtr = IntPtr.Zero;
  41. //private IntPtr JieTuBitmapPtr = IntPtr.Zero;
  42. /// <summary>
  43. /// 截图
  44. /// </summary>
  45. public JieTuWindow()
  46. {
  47. _Current = this;
  48. InitializeComponent();
  49. DataContext = new AppModel();
  50. initialization();
  51. Screenshot();
  52. MaxWindow();
  53. MaskLeft.Height = ScreenHeight;
  54. MaskRight.Height = ScreenHeight;
  55. //计算Windows项目缩放比例
  56. ScreenHelper.ResetScreenScale();
  57. //RemoveControl();
  58. //AllowsTransparency = "True"
  59. }
  60. public void Screenshot()
  61. {
  62. //IntPtr BitmapPtr = IntPtr.Zero;
  63. //Background = new ImageBrush(ImageHelper.GetFullBitmapSource(out BitmapPtr));
  64. string time = GetTimeStamp();
  65. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
  66. if (!Directory.Exists(tempPath))
  67. {
  68. Directory.CreateDirectory(tempPath);
  69. }
  70. string imagePath = Path.Combine(tempPath, time + ".jpg");
  71. if (ImageHelper.GetScreenshot(new Rectangle(0, 0, 0, 0), null, true, out BitmapImage bitmap))
  72. {
  73. Background = new ImageBrush(bitmap);
  74. }
  75. }
  76. public void initialization()
  77. {
  78. Background = null;
  79. _IsMouseDown = false;
  80. _IsCapture = false;
  81. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  82. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  83. SizeRGB.Visibility = Visibility.Collapsed;
  84. MainImage.Visibility = Visibility.Collapsed;
  85. DataContext = new AppModel();
  86. WpfHelper.MainDispatcher = Dispatcher;
  87. }
  88. #region 属性 Current
  89. private static JieTuWindow _Current = null;
  90. public static JieTuWindow Current => _Current;
  91. #endregion 属性 Current
  92. #region 全屏+置顶
  93. private void MaxWindow()
  94. {
  95. Left = 0;
  96. Top = 0;
  97. Width = ScreenWidth;
  98. Height = ScreenHeight;
  99. Activate();
  100. }
  101. #endregion 全屏+置顶
  102. #region 注册画图
  103. public static void Register(object control)
  104. {
  105. string name = "Draw" + _Current.num;
  106. _Current.MainCanvas.RegisterName(name, control);
  107. _Current.list.Add(new NameAndLimit(name));
  108. _Current.num++;
  109. }
  110. #endregion 注册画图
  111. #region 截图区域添加画图
  112. public static void AddControl(UIElement e)
  113. {
  114. _Current.MainCanvas.Children.Add(e);
  115. }
  116. #endregion 截图区域添加画图
  117. #region 截图区域撤回画图
  118. public static void RemoveControl(UIElement e)
  119. {
  120. _Current.MainCanvas.Children.Remove(e);
  121. }
  122. #endregion 截图区域撤回画图
  123. #region 撤销
  124. public void OnRevoke()
  125. {
  126. if (list.Count > 0)
  127. {
  128. string name = list[list.Count - 1].Name;
  129. object obj = MainCanvas.FindName(name);
  130. if (obj != null)
  131. {
  132. MainCanvas.Children.Remove(obj as UIElement);
  133. MainCanvas.UnregisterName(name);
  134. list.RemoveAt(list.Count - 1);
  135. MainImage.Limit = list.Count == 0 ? new Limit() : list[list.Count - 1].Limit;
  136. }
  137. }
  138. }
  139. #endregion 撤销
  140. #region 保存
  141. public void OnSave()
  142. {
  143. //var sfd = new Microsoft.Win32.SaveFileDialog
  144. //{
  145. // FileName = "截图" + DateTime.Now.ToString("yyyyMMddhhmmss"),
  146. // Filter = "png|*.png",
  147. // AddExtension = true,
  148. // RestoreDirectory = true
  149. //};
  150. //if (sfd.ShowDialog() == true)
  151. //{
  152. // Hidden();
  153. // Thread t = new Thread(new ThreadStart(() =>
  154. // {
  155. // Thread.Sleep(200);
  156. // WpfHelper.SafeRun(() =>
  157. // {
  158. // var source = GetCapture();
  159. // if (source != null)
  160. // {
  161. // ImageHelper.SaveToPng(source, sfd.FileName);
  162. // Shared.TeachingData.FilePath = sfd.FileName;
  163. // //Thread.Sleep(200);
  164. // }
  165. // Close();
  166. // if (ChangeTextEvent != null)
  167. // {
  168. // ChangeTextEvent("关闭窗口");
  169. // }
  170. // //Go(sfd.FileName);
  171. // });
  172. // }))
  173. // {
  174. // IsBackground = true
  175. // };
  176. // t.Start();
  177. //}
  178. }
  179. #endregion 保存
  180. //定义事件
  181. public event ChangeTextHandler ChangeTextEvent;
  182. #region 获取截图
  183. private void GetCapture()
  184. {
  185. string time = GetTimeStamp();
  186. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
  187. if (!Directory.Exists(tempPath))
  188. {
  189. Directory.CreateDirectory(tempPath);
  190. }
  191. APP.ImgPath = string.Empty;
  192. string imagePath = Path.Combine(tempPath, time + ".jpg");
  193. APP.ImgPath = imagePath;
  194. //LogHelper.WriteInfoLog(imagePath);
  195. //this.Close();
  196. //string imagePath = ImageHelper.GetImagePath(out string serverSavePath);
  197. //BitmapSource bitmap = ImageHelper.GetBitmapSource((int)AppModel.Current.MaskLeftWidth + 1, (int)AppModel.Current.MaskTopHeight + 1, (int)MainImage.ActualWidth - 2, (int)MainImage.ActualHeight - 2, out JieTuBitmapPtr);
  198. /* BitmapImage bitmap = */
  199. ImageHelper.GetScreenshot(new Rectangle((int)AppModel.Current.MaskLeftWidth + 1, (int)AppModel.Current.MaskTopHeight + 1, (int)MainImage.ActualWidth - 2, (int)MainImage.ActualHeight - 2), imagePath, true, out BitmapImage bitmapimg);
  200. _IsMouseDown = false;
  201. _IsCapture = false;
  202. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  203. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  204. SizeRGB.Visibility = Visibility.Collapsed;
  205. MainImage.Visibility = Visibility.Collapsed;
  206. DataContext = new AppModel();
  207. //ImageHelper.GetScreenshot(new Rectangle(0, 0, 0, 0), imagePath, true, out BitmapImage bitmap);//zxy
  208. //Background = new ImageBrush(bitmap);
  209. WpfHelper.MainDispatcher = Dispatcher;
  210. MaxWindow();
  211. MaskLeft.Height = ScreenHeight;
  212. MaskRight.Height = ScreenHeight;
  213. //计算Windows项目缩放比例
  214. ScreenHelper.ResetScreenScale();
  215. //if (bitmap != null)
  216. //{
  217. // //Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = serverSavePath;
  218. // //Shared.TeachingData.FilePath = imagePath;
  219. //}
  220. ///修改为统一截图时存储到服务器 修改人:赵耀 修改时间:2020年8月11日
  221. //if (bitmap != null)
  222. //{
  223. // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  224. // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
  225. // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
  226. // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
  227. // Shared.TeachingData.FilePath = path;
  228. // FileInfo fi = new FileInfo(path); var di = fi.Directory; if (!di.Exists) {
  229. // di.Create();
  230. //}
  231. // using (var fs = System.IO.File.Create(path))
  232. // {
  233. // BitmapEncoder encoder = new PngBitmapEncoder();
  234. // encoder.Frames.Add(BitmapFrame.Create(bitmap));
  235. // encoder.Save(fs);
  236. // bitmap = null;
  237. // encoder = null;
  238. // }
  239. //}
  240. //Dispose();
  241. //GC.Collect();
  242. //ImageHelper.DeleteObject(BitmapPtr);
  243. //ImageHelper.DeleteObject(JieTuBitmapPtr);
  244. initialization();
  245. Owner = null;
  246. WindowState = WindowState.Minimized;
  247. Visibility = Visibility.Hidden;
  248. ShowInTaskbar = false;
  249. //this.Hide();
  250. if (ChangeTextEvent != null)
  251. {
  252. ChangeTextEvent("关闭窗口");
  253. }
  254. }
  255. #endregion 获取截图
  256. /// <summary>
  257. /// 获取时间戳
  258. /// </summary>
  259. /// <returns></returns>
  260. public string GetTimeStamp()
  261. {
  262. TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  263. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  264. }
  265. #region 退出截图
  266. public void OnCancel()
  267. {
  268. //Close();
  269. //ImageHelper.DeleteObject(BitmapPtr);
  270. //ImageHelper.DeleteObject(JieTuBitmapPtr);
  271. initialization();
  272. Owner = null;
  273. WindowState = WindowState.Minimized;
  274. Visibility = Visibility.Hidden;
  275. ShowInTaskbar = false;
  276. //this.Hide();
  277. //_Bitmap.Dispose();
  278. }
  279. #endregion 退出截图
  280. #region 完成截图
  281. public void OnOK()
  282. {
  283. Hidden();
  284. Thread t = new Thread(new ThreadStart(() =>
  285. {
  286. Thread.Sleep(50);
  287. //Thread.Sleep(200);
  288. WpfHelper.SafeRun(() =>
  289. {
  290. /*var source = */
  291. GetCapture();
  292. //Shared.TeachingData.Source = source;
  293. //if (source != null)
  294. //{
  295. // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  296. // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
  297. // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
  298. // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
  299. // ImageHelper.SaveToPng(source, path);
  300. // Shared.TeachingData.FilePath = path;
  301. // //Clipboard.SetImage(source);
  302. //}
  303. //Close();
  304. //if (ChangeTextEvent != null)
  305. //{
  306. // ChangeTextEvent("关闭窗口");
  307. //}
  308. });
  309. }))
  310. {
  311. IsBackground = true
  312. };
  313. t.Start();
  314. }
  315. #endregion 完成截图
  316. #region 截图前隐藏窗口
  317. private void Hidden()
  318. {
  319. //隐藏尺寸RGB框
  320. if (AppModel.Current.MaskTopHeight < 40)
  321. {
  322. SizeRGB.Visibility = Visibility.Collapsed;
  323. }
  324. int need = SizeColorBar.Current.Selected == Tool.Null ? 30 : 67;
  325. if (AppModel.Current.MaskBottomHeight < need && AppModel.Current.MaskTopHeight < need)
  326. {
  327. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  328. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  329. }
  330. MainImage.ZoomThumbVisibility = Visibility.Collapsed;
  331. }
  332. #endregion 截图前隐藏窗口
  333. #region 鼠标及键盘事件
  334. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  335. {
  336. if (_IsCapture)
  337. {
  338. return;
  339. }
  340. System.Windows.Point point = e.GetPosition(this);
  341. _X0 = point.X;
  342. _Y0 = point.Y;
  343. _IsMouseDown = true;
  344. Canvas.SetLeft(MainImage, _X0);
  345. Canvas.SetTop(MainImage, _Y0);
  346. AppModel.Current.MaskLeftWidth = _X0;
  347. AppModel.Current.MaskRightWidth = ScreenWidth - _X0;
  348. AppModel.Current.MaskTopHeight = _Y0;
  349. Show_Size.Visibility = Visibility.Visible;
  350. }
  351. private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  352. {
  353. if (!_IsMouseDown || _IsCapture)
  354. {
  355. return;
  356. }
  357. _IsMouseDown = false;
  358. if (MainImage.Width >= MinSize && MainImage.Height >= MinSize)
  359. {
  360. _IsCapture = true;
  361. ImageEditBar.Current.Visibility = Visibility.Visible;
  362. ImageEditBar.Current.ResetCanvas();
  363. Cursor = Cursors.Arrow;
  364. }
  365. }
  366. private void Window_MouseMove(object sender, MouseEventArgs e)
  367. {
  368. System.Windows.Point point = e.GetPosition(this);
  369. System.Windows.Point screenP = PointToScreen(point);
  370. try
  371. {
  372. AppModel.Current.ShowRGB = ImageHelper.GetRGB((int)screenP.X, (int)screenP.Y);
  373. }
  374. catch (Exception ex)
  375. {
  376. MessageWindow.Show(ex.Message);
  377. }
  378. if (_IsCapture)
  379. {
  380. return;
  381. }
  382. if (Show_RGB.Visibility == Visibility.Collapsed)
  383. {
  384. Show_RGB.Visibility = Visibility.Visible;
  385. }
  386. if (_IsMouseDown)
  387. {
  388. double w = point.X - _X0;
  389. double h = point.Y - _Y0;
  390. if (w < MinSize || h < MinSize)
  391. {
  392. return;
  393. }
  394. if (MainImage.Visibility == Visibility.Collapsed)
  395. {
  396. MainImage.Visibility = Visibility.Visible;
  397. }
  398. AppModel.Current.MaskRightWidth = ScreenWidth - point.X;
  399. AppModel.Current.MaskTopWidth = w;
  400. AppModel.Current.MaskBottomHeight = ScreenHeight - point.Y;
  401. AppModel.Current.ChangeShowSize();
  402. MainImage.Width = w;
  403. MainImage.Height = h;
  404. }
  405. else
  406. {
  407. AppModel.Current.ShowSizeLeft = point.X;
  408. AppModel.Current.ShowSizeTop = ScreenHeight - point.Y < 30 ? point.Y - 30 : point.Y + 10;
  409. }
  410. }
  411. private void Window_KeyDown(object sender, KeyEventArgs e)
  412. {
  413. if (e.Key == Key.Escape)
  414. {
  415. initialization();
  416. Owner = null;
  417. WindowState = WindowState.Minimized;
  418. Visibility = Visibility.Hidden;
  419. ShowInTaskbar = false;
  420. }
  421. }
  422. #endregion 鼠标及键盘事件
  423. /// <summary>
  424. /// 关闭时清除tool页记录的截图数据
  425. /// </summary>
  426. /// <param name="sender"></param>
  427. /// <param name="e"></param>
  428. private void Window_Closed(object sender, EventArgs e)
  429. {
  430. click_closeJietuWindowClick();
  431. }
  432. /// <summary>
  433. /// 非正常关闭时jietuWindow=null;
  434. /// </summary>
  435. /// <param name="sender"></param>
  436. /// <param name="e"></param>
  437. public delegate void CloseJietuWindowClick();
  438. /// <summary>
  439. /// 非正常关闭时jietuWindow=null;
  440. /// </summary>
  441. public event CloseJietuWindowClick click_closeJietuWindowClick;
  442. }
  443. }