星火微课系统客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

JieTuWindow.xaml.cs 17KB

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