星火微课系统客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

JieTuWindow.xaml.cs 17KB

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