|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
-
- using ComeCapture.Controls;
- using ComeCapture.Helpers;
- using ComeCapture.Models;
-
- using Common.system;
-
-
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
-
- using XHWK.WKTool;
-
- using static XHWK.WKTool.MainWindow;
-
- namespace ComeCapture
- {
- /// <summary>
- /// 截图 MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class JieTuWindow : Window
- {
- public static double ScreenWidth = SystemParameters.PrimaryScreenWidth;
- public static double ScreenHeight = SystemParameters.PrimaryScreenHeight;
- public static double ScreenScale = 1;
- public static int MinSize = 10;
- internal double pwidth = SystemParameters.PrimaryScreenWidth;
-
- internal double pHeight = SystemParameters.PrimaryScreenHeight;
-
- //画图注册名称集合
- public List<NameAndLimit> list = new List<NameAndLimit>();
-
- //画图注册名称
- public int num = 1;
-
- //是否截图开始
- private bool _IsMouseDown = false;
-
- //是否截图完毕
- private bool _IsCapture = false;
-
- private double _X0 = 0;
- private double _Y0 = 0;
- /// <summary>
- /// 键盘钩子
- /// </summary>
- private KeyboardHookCommon k_hook;
- //private IntPtr BitmapPtr = IntPtr.Zero;
- //private IntPtr JieTuBitmapPtr = IntPtr.Zero;
- /// <summary>
- /// 截图
- /// </summary>
- public JieTuWindow()
- {
- _Current = this;
- InitializeComponent();
- k_hook = new KeyboardHookCommon();
- k_hook.KeyDownEvent += K_hook_KeyDownEvent;
- DataContext = new AppModel();
- initialization();
- Screenshot();
- MaxWindow();
- MaskLeft.Height = ScreenHeight;
- MaskRight.Height = ScreenHeight;
- //计算Windows项目缩放比例
- ScreenHelper.ResetScreenScale();
- //RemoveControl();
- //AllowsTransparency = "True"
- }
-
- public void Screenshot()
- {
- //IntPtr BitmapPtr = IntPtr.Zero;
- //Background = new ImageBrush(ImageHelper.GetFullBitmapSource(out BitmapPtr));
-
- string time = GetTimeStamp();
- string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
- if (!Directory.Exists(tempPath))
- {
- Directory.CreateDirectory(tempPath);
- }
- string imagePath = Path.Combine(tempPath, time + ".jpg");
- if (ImageHelper.GetScreenshot(new Rectangle(0, 0, 0, 0), null, true, out BitmapImage bitmap))
- {
- Background = new ImageBrush(bitmap);
- }
- }
-
- public void initialization()
- {
- Background = null;
- _IsMouseDown = false;
- _IsCapture = false;
- ImageEditBar.Current.Visibility = Visibility.Collapsed;
- SizeColorBar.Current.Visibility = Visibility.Collapsed;
- SizeRGB.Visibility = Visibility.Collapsed;
- MainImage.Visibility = Visibility.Collapsed;
- DataContext = new AppModel();
- WpfHelper.MainDispatcher = Dispatcher;
- }
- /// <summary>
- /// 初始化快捷键
- /// </summary>
- public void InitializeKeyDownEvent()
- {
-
- k_hook.Start();//安装键盘钩子
- }
- /// <summary>
- /// 快捷键
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void K_hook_KeyDownEvent(object sender, System.Windows.Forms.KeyEventArgs e)
- {
- if (e.KeyValue == (int)System.Windows.Forms.Keys.Escape)
- {
- k_hook.Stop();
- Current.OnCancel();
- App.W_XHMicroLessonSystemWindow.InitializeKeyDownEvent();
- App.W_XHMicroLessonSystemWindow.WindowType();
- Hide();
- }
- }
-
- #region 属性 Current
-
- private static JieTuWindow _Current = null;
-
- public static JieTuWindow Current => _Current;
-
- #endregion 属性 Current
-
- #region 全屏+置顶
-
- private void MaxWindow()
- {
- Left = 0;
- Top = 0;
- Width = ScreenWidth;
- Height = ScreenHeight;
- Activate();
- }
-
- #endregion 全屏+置顶
-
- #region 注册画图
-
- public static void Register(object control)
- {
- string name = "Draw" + _Current.num;
- _Current.MainCanvas.RegisterName(name, control);
- _Current.list.Add(new NameAndLimit(name));
- _Current.num++;
- }
-
- #endregion 注册画图
-
- #region 截图区域添加画图
-
- public static void AddControl(UIElement e)
- {
- _Current.MainCanvas.Children.Add(e);
- }
-
- #endregion 截图区域添加画图
-
- #region 截图区域撤回画图
-
- public static void RemoveControl(UIElement e)
- {
- _Current.MainCanvas.Children.Remove(e);
- }
-
- #endregion 截图区域撤回画图
-
- #region 撤销
-
- public void OnRevoke()
- {
- if (list.Count > 0)
- {
- string name = list[list.Count - 1].Name;
- object obj = MainCanvas.FindName(name);
- if (obj != null)
- {
- MainCanvas.Children.Remove(obj as UIElement);
- MainCanvas.UnregisterName(name);
- list.RemoveAt(list.Count - 1);
- MainImage.Limit = list.Count == 0 ? new Limit() : list[list.Count - 1].Limit;
- }
- }
- }
-
- #endregion 撤销
-
- #region 保存
-
- public void OnSave()
- {
- //var sfd = new Microsoft.Win32.SaveFileDialog
- //{
- // FileName = "截图" + DateTime.Now.ToString("yyyyMMddhhmmss"),
- // Filter = "png|*.png",
- // AddExtension = true,
- // RestoreDirectory = true
- //};
- //if (sfd.ShowDialog() == true)
- //{
- // Hidden();
- // Thread t = new Thread(new ThreadStart(() =>
- // {
- // Thread.Sleep(200);
- // WpfHelper.SafeRun(() =>
- // {
- // var source = GetCapture();
- // if (source != null)
- // {
- // ImageHelper.SaveToPng(source, sfd.FileName);
- // Shared.TeachingData.FilePath = sfd.FileName;
- // //Thread.Sleep(200);
- // }
- // Close();
- // if (ChangeTextEvent != null)
- // {
- // ChangeTextEvent("关闭窗口");
- // }
- // //Go(sfd.FileName);
- // });
- // }))
- // {
- // IsBackground = true
- // };
- // t.Start();
- //}
- }
-
- #endregion 保存
-
- //定义事件
- public event ChangeTextHandler ChangeTextEvent;
-
- #region 获取截图
-
-
- private void GetCapture()
- {
-
- string time = GetTimeStamp();
- string tempPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
- if (!Directory.Exists(tempPath))
- {
- Directory.CreateDirectory(tempPath);
- }
- App.ImgPath = string.Empty;
- string imagePath = Path.Combine(tempPath, time + ".jpg");
- App.ImgPath = imagePath;
- //LogHelper.WriteInfoLog(imagePath);
- //this.Close();
- //string imagePath = ImageHelper.GetImagePath(out string serverSavePath);
-
- //BitmapSource bitmap = ImageHelper.GetBitmapSource((int)AppModel.Current.MaskLeftWidth + 1, (int)AppModel.Current.MaskTopHeight + 1, (int)MainImage.ActualWidth - 2, (int)MainImage.ActualHeight - 2, out JieTuBitmapPtr);
- /* 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, true, out BitmapImage bitmapimg);
- _IsMouseDown = false;
- _IsCapture = false;
- ImageEditBar.Current.Visibility = Visibility.Collapsed;
- SizeColorBar.Current.Visibility = Visibility.Collapsed;
- SizeRGB.Visibility = Visibility.Collapsed;
- MainImage.Visibility = Visibility.Collapsed;
- DataContext = new AppModel();
- //ImageHelper.GetScreenshot(new Rectangle(0, 0, 0, 0), imagePath, true, out BitmapImage bitmap);//zxy
- //Background = new ImageBrush(bitmap);
- WpfHelper.MainDispatcher = Dispatcher;
- MaxWindow();
- MaskLeft.Height = ScreenHeight;
- MaskRight.Height = ScreenHeight;
- //计算Windows项目缩放比例
- ScreenHelper.ResetScreenScale();
- //if (bitmap != null)
- //{
- // //Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = serverSavePath;
- // //Shared.TeachingData.FilePath = imagePath;
- //}
-
- ///修改为统一截图时存储到服务器 修改人:赵耀 修改时间:2020年8月11日
- //if (bitmap != null)
- //{
- // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
- // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
- // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
- // Shared.TeachingData.FilePath = path;
- // FileInfo fi = new FileInfo(path); var di = fi.Directory; if (!di.Exists) {
- // di.Create();
- //}
-
- // using (var fs = System.IO.File.Create(path))
- // {
- // BitmapEncoder encoder = new PngBitmapEncoder();
- // encoder.Frames.Add(BitmapFrame.Create(bitmap));
- // encoder.Save(fs);
- // bitmap = null;
- // encoder = null;
- // }
- //}
- //Dispose();
- //GC.Collect();
- //ImageHelper.DeleteObject(BitmapPtr);
- //ImageHelper.DeleteObject(JieTuBitmapPtr);
- initialization();
- Owner = null;
- WindowState = WindowState.Minimized;
- Visibility = Visibility.Hidden;
- ShowInTaskbar = false;
- //this.Hide();
- if (ChangeTextEvent != null)
- {
- k_hook.Stop();
- ChangeTextEvent("关闭窗口");
- }
- }
-
- #endregion 获取截图
- /// <summary>
- /// 获取时间戳
- /// </summary>
- /// <returns></returns>
- public string GetTimeStamp()
- {
- TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- return Convert.ToInt64(ts.TotalMilliseconds).ToString();
- }
- #region 退出截图
-
- public void OnCancel()
- {
- //Close();
- //ImageHelper.DeleteObject(BitmapPtr);
- //ImageHelper.DeleteObject(JieTuBitmapPtr);
- initialization();
- Owner = null;
- WindowState = WindowState.Minimized;
- Visibility = Visibility.Hidden;
- ShowInTaskbar = false;
- //this.Hide();
- //_Bitmap.Dispose();
-
- if (ChangeTextEvent != null)
- {
- k_hook.Stop();
- ChangeTextEvent("取消");
- }
- }
-
- #endregion 退出截图
-
- #region 完成截图
-
- public void OnOK()
- {
- Hidden();
-
- Thread t = new Thread(new ThreadStart(() =>
- {
- Thread.Sleep(50);
- //Thread.Sleep(200);
- WpfHelper.SafeRun(() =>
- {
- /*var source = */
- GetCapture();
- //Shared.TeachingData.Source = source;
- //if (source != null)
- //{
- // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
- // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
- // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
- // ImageHelper.SaveToPng(source, path);
- // Shared.TeachingData.FilePath = path;
- // //Clipboard.SetImage(source);
- //}
- //Close();
- //if (ChangeTextEvent != null)
- //{
- // ChangeTextEvent("关闭窗口");
- //}
- });
- }))
- {
- IsBackground = true
- };
- t.Start();
- }
-
- #endregion 完成截图
-
- #region 截图前隐藏窗口
-
- private void Hidden()
- {
- //隐藏尺寸RGB框
- if (AppModel.Current.MaskTopHeight < 40)
- {
- SizeRGB.Visibility = Visibility.Collapsed;
- }
- int need = SizeColorBar.Current.Selected == Tool.Null ? 30 : 67;
- if (AppModel.Current.MaskBottomHeight < need && AppModel.Current.MaskTopHeight < need)
- {
- ImageEditBar.Current.Visibility = Visibility.Collapsed;
- SizeColorBar.Current.Visibility = Visibility.Collapsed;
- }
- MainImage.ZoomThumbVisibility = Visibility.Collapsed;
- }
-
- #endregion 截图前隐藏窗口
-
- #region 鼠标及键盘事件
-
- private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (_IsCapture)
- {
- return;
- }
- System.Windows.Point point = e.GetPosition(this);
- _X0 = point.X;
- _Y0 = point.Y;
- _IsMouseDown = true;
- Canvas.SetLeft(MainImage, _X0);
- Canvas.SetTop(MainImage, _Y0);
- AppModel.Current.MaskLeftWidth = _X0;
- AppModel.Current.MaskRightWidth = ScreenWidth - _X0;
- AppModel.Current.MaskTopHeight = _Y0;
- Show_Size.Visibility = Visibility.Visible;
- }
-
- private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (!_IsMouseDown || _IsCapture)
- {
- return;
- }
- _IsMouseDown = false;
- if (MainImage.Width >= MinSize && MainImage.Height >= MinSize)
- {
- _IsCapture = true;
- ImageEditBar.Current.Visibility = Visibility.Visible;
- ImageEditBar.Current.ResetCanvas();
- Cursor = Cursors.Arrow;
- }
- }
-
- private void Window_MouseMove(object sender, MouseEventArgs e)
- {
- System.Windows.Point point = e.GetPosition(this);
- System.Windows.Point screenP = PointToScreen(point);
- try
- {
- AppModel.Current.ShowRGB = ImageHelper.GetRGB((int)screenP.X, (int)screenP.Y);
- }
- catch (Exception ex)
- {
- MessageWindow.Show(ex.Message);
- }
- if (_IsCapture)
- {
- return;
- }
-
- if (Show_RGB.Visibility == Visibility.Collapsed)
- {
- Show_RGB.Visibility = Visibility.Visible;
- }
-
- if (_IsMouseDown)
- {
- double w = point.X - _X0;
- double h = point.Y - _Y0;
- if (w < MinSize || h < MinSize)
- {
- return;
- }
- if (MainImage.Visibility == Visibility.Collapsed)
- {
- MainImage.Visibility = Visibility.Visible;
- }
- AppModel.Current.MaskRightWidth = ScreenWidth - point.X;
- AppModel.Current.MaskTopWidth = w;
- AppModel.Current.MaskBottomHeight = ScreenHeight - point.Y;
- AppModel.Current.ChangeShowSize();
- MainImage.Width = w;
- MainImage.Height = h;
- }
- else
- {
- AppModel.Current.ShowSizeLeft = point.X;
- AppModel.Current.ShowSizeTop = ScreenHeight - point.Y < 30 ? point.Y - 30 : point.Y + 10;
- }
- }
-
- private void Window_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Escape)
- {
- initialization();
- Owner = null;
- WindowState = WindowState.Minimized;
- Visibility = Visibility.Hidden;
- ShowInTaskbar = false;
- }
- }
-
- #endregion 鼠标及键盘事件
-
- /// <summary>
- /// 关闭时清除tool页记录的截图数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Window_Closed(object sender, EventArgs e)
- {
- click_closeJietuWindowClick();
- }
-
- /// <summary>
- /// 非正常关闭时jietuWindow=null;
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public delegate void CloseJietuWindowClick();
-
- /// <summary>
- /// 非正常关闭时jietuWindow=null;
- /// </summary>
- public event CloseJietuWindowClick click_closeJietuWindowClick;
- }
- }
|