星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JieTuWindow.xaml.cs 17KB

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