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