星火微课系统客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JieTuWindow.xaml.cs 17KB

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