星火微课系统客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JieTuWindow.xaml.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 = */
  197. 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. MessageWindow.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. }