星火微课系统客户端
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

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