星火微课系统客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

JieTuWindow.xaml.cs 17KB

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