星火微课系统客户端
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JieTuWindow.xaml.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. Background = new ImageBrush(bitmap);
  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,true,out BitmapImage bitmapimg);
  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. ImageHelper.GetScreenshot(new Rectangle(0, 0, 0, 0), imagePath, true, out BitmapImage bitmap);//zxy
  205. Background = new ImageBrush(bitmap);
  206. WpfHelper.MainDispatcher = Dispatcher;
  207. MaxWindow();
  208. MaskLeft.Height = ScreenHeight;
  209. MaskRight.Height = ScreenHeight;
  210. //计算Windows项目缩放比例
  211. ScreenHelper.ResetScreenScale();
  212. //if (bitmap != null)
  213. //{
  214. // //Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = serverSavePath;
  215. // //Shared.TeachingData.FilePath = imagePath;
  216. //}
  217. ///修改为统一截图时存储到服务器 修改人:赵耀 修改时间:2020年8月11日
  218. //if (bitmap != null)
  219. //{
  220. // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  221. // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
  222. // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
  223. // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
  224. // Shared.TeachingData.FilePath = path;
  225. // FileInfo fi = new FileInfo(path); var di = fi.Directory; if (!di.Exists) {
  226. // di.Create();
  227. //}
  228. // using (var fs = System.IO.File.Create(path))
  229. // {
  230. // BitmapEncoder encoder = new PngBitmapEncoder();
  231. // encoder.Frames.Add(BitmapFrame.Create(bitmap));
  232. // encoder.Save(fs);
  233. // bitmap = null;
  234. // encoder = null;
  235. // }
  236. //}
  237. //Dispose();
  238. //GC.Collect();
  239. //ImageHelper.DeleteObject(BitmapPtr);
  240. //ImageHelper.DeleteObject(JieTuBitmapPtr);
  241. initialization();
  242. Owner = null;
  243. WindowState = WindowState.Minimized;
  244. Visibility = Visibility.Hidden;
  245. ShowInTaskbar = false;
  246. //this.Hide();
  247. if (ChangeTextEvent != null)
  248. {
  249. ChangeTextEvent("关闭窗口");
  250. }
  251. }
  252. #endregion 获取截图
  253. /// <summary>
  254. /// 获取时间戳
  255. /// </summary>
  256. /// <returns></returns>
  257. public string GetTimeStamp()
  258. {
  259. TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  260. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  261. }
  262. #region 退出截图
  263. public void OnCancel()
  264. {
  265. //Close();
  266. //ImageHelper.DeleteObject(BitmapPtr);
  267. //ImageHelper.DeleteObject(JieTuBitmapPtr);
  268. initialization();
  269. Owner = null;
  270. WindowState = WindowState.Minimized;
  271. Visibility = Visibility.Hidden;
  272. ShowInTaskbar = false;
  273. //this.Hide();
  274. //_Bitmap.Dispose();
  275. }
  276. #endregion 退出截图
  277. #region 完成截图
  278. public void OnOK()
  279. {
  280. Hidden();
  281. Thread t = new Thread(new ThreadStart(() =>
  282. {
  283. Thread.Sleep(50);
  284. //Thread.Sleep(200);
  285. WpfHelper.SafeRun(() =>
  286. {
  287. /*var source = */
  288. GetCapture();
  289. //Shared.TeachingData.Source = source;
  290. //if (source != null)
  291. //{
  292. // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  293. // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
  294. // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
  295. // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
  296. // ImageHelper.SaveToPng(source, path);
  297. // Shared.TeachingData.FilePath = path;
  298. // //Clipboard.SetImage(source);
  299. //}
  300. //Close();
  301. //if (ChangeTextEvent != null)
  302. //{
  303. // ChangeTextEvent("关闭窗口");
  304. //}
  305. });
  306. }))
  307. {
  308. IsBackground = true
  309. };
  310. t.Start();
  311. }
  312. #endregion 完成截图
  313. #region 截图前隐藏窗口
  314. private void Hidden()
  315. {
  316. //隐藏尺寸RGB框
  317. if (AppModel.Current.MaskTopHeight < 40)
  318. {
  319. SizeRGB.Visibility = Visibility.Collapsed;
  320. }
  321. int need = SizeColorBar.Current.Selected == Tool.Null ? 30 : 67;
  322. if (AppModel.Current.MaskBottomHeight < need && AppModel.Current.MaskTopHeight < need)
  323. {
  324. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  325. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  326. }
  327. MainImage.ZoomThumbVisibility = Visibility.Collapsed;
  328. }
  329. #endregion 截图前隐藏窗口
  330. #region 鼠标及键盘事件
  331. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  332. {
  333. if (_IsCapture)
  334. {
  335. return;
  336. }
  337. System.Windows.Point point = e.GetPosition(this);
  338. _X0 = point.X;
  339. _Y0 = point.Y;
  340. _IsMouseDown = true;
  341. Canvas.SetLeft(MainImage, _X0);
  342. Canvas.SetTop(MainImage, _Y0);
  343. AppModel.Current.MaskLeftWidth = _X0;
  344. AppModel.Current.MaskRightWidth = ScreenWidth - _X0;
  345. AppModel.Current.MaskTopHeight = _Y0;
  346. Show_Size.Visibility = Visibility.Visible;
  347. }
  348. private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  349. {
  350. if (!_IsMouseDown || _IsCapture)
  351. {
  352. return;
  353. }
  354. _IsMouseDown = false;
  355. if (MainImage.Width >= MinSize && MainImage.Height >= MinSize)
  356. {
  357. _IsCapture = true;
  358. ImageEditBar.Current.Visibility = Visibility.Visible;
  359. ImageEditBar.Current.ResetCanvas();
  360. Cursor = Cursors.Arrow;
  361. }
  362. }
  363. private void Window_MouseMove(object sender, MouseEventArgs e)
  364. {
  365. System.Windows.Point point = e.GetPosition(this);
  366. System.Windows.Point screenP = PointToScreen(point);
  367. try
  368. {
  369. AppModel.Current.ShowRGB = ImageHelper.GetRGB((int)screenP.X, (int)screenP.Y);
  370. }
  371. catch (Exception ex)
  372. {
  373. MessageWindow.Show(ex.Message);
  374. }
  375. if (_IsCapture)
  376. {
  377. return;
  378. }
  379. if (Show_RGB.Visibility == Visibility.Collapsed)
  380. {
  381. Show_RGB.Visibility = Visibility.Visible;
  382. }
  383. if (_IsMouseDown)
  384. {
  385. double w = point.X - _X0;
  386. double h = point.Y - _Y0;
  387. if (w < MinSize || h < MinSize)
  388. {
  389. return;
  390. }
  391. if (MainImage.Visibility == Visibility.Collapsed)
  392. {
  393. MainImage.Visibility = Visibility.Visible;
  394. }
  395. AppModel.Current.MaskRightWidth = ScreenWidth - point.X;
  396. AppModel.Current.MaskTopWidth = w;
  397. AppModel.Current.MaskBottomHeight = ScreenHeight - point.Y;
  398. AppModel.Current.ChangeShowSize();
  399. MainImage.Width = w;
  400. MainImage.Height = h;
  401. }
  402. else
  403. {
  404. AppModel.Current.ShowSizeLeft = point.X;
  405. AppModel.Current.ShowSizeTop = ScreenHeight - point.Y < 30 ? point.Y - 30 : point.Y + 10;
  406. }
  407. }
  408. private void Window_KeyDown(object sender, KeyEventArgs e)
  409. {
  410. if (e.Key == Key.Escape)
  411. {
  412. initialization();
  413. Owner = null;
  414. WindowState = WindowState.Minimized;
  415. Visibility = Visibility.Hidden;
  416. ShowInTaskbar = false;
  417. }
  418. }
  419. #endregion 鼠标及键盘事件
  420. /// <summary>
  421. /// 关闭时清除tool页记录的截图数据
  422. /// </summary>
  423. /// <param name="sender"></param>
  424. /// <param name="e"></param>
  425. private void Window_Closed(object sender, EventArgs e)
  426. {
  427. click_closeJietuWindowClick();
  428. }
  429. /// <summary>
  430. /// 非正常关闭时jietuWindow=null;
  431. /// </summary>
  432. /// <param name="sender"></param>
  433. /// <param name="e"></param>
  434. public delegate void CloseJietuWindowClick();
  435. /// <summary>
  436. /// 非正常关闭时jietuWindow=null;
  437. /// </summary>
  438. public event CloseJietuWindowClick click_closeJietuWindowClick;
  439. }
  440. }