ImgWindow.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using MeterVision.Config;
  2. using Ookii.Dialogs.Wpf;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Interop;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. //using System.Windows.Shapes;
  19. namespace MeterVision
  20. {
  21. /// <summary>
  22. /// imgWindow.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class imgWindow : Window
  25. {
  26. [DllImport("user32.dll")]
  27. private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
  28. [DllImport("user32.dll")]
  29. private static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);
  30. private const uint SC_MINIMIZE = 0xF020;
  31. private const uint MF_BYCOMMAND = 0x00000000;
  32. private double zoomFactor = 1.0;
  33. private double rotationAngle = 0;
  34. private Point? dragStartPoint; // 用于记录鼠标按下的位置
  35. private string _imageSourcePath;
  36. public string ImageSourcePath => _imageSourcePath;
  37. public imgWindow(string imageSourcePath)
  38. {
  39. InitializeComponent();
  40. int width = CfginiItem.GetConfigItem().DialogImageSize_Width;
  41. int height = CfginiItem.GetConfigItem().DialogImageSize_Height;
  42. if(width != 0 && height != 0)
  43. {
  44. this.Width = width;
  45. this.Height = height;
  46. }
  47. _imageSourcePath = imageSourcePath;
  48. if (File.Exists(imageSourcePath))
  49. {
  50. imgViewer.Source = new BitmapImage(new Uri(imageSourcePath, UriKind.RelativeOrAbsolute));
  51. }
  52. DataContext = this;
  53. }
  54. // 鼠标点击外部时关闭窗口
  55. private void OnMouseDownOutside(object sender, MouseButtonEventArgs e)
  56. {
  57. //this.Close();
  58. }
  59. private void ImgWindow_Deactivated(object sender, EventArgs e)
  60. {
  61. //throw new NotImplementedException();
  62. //this.Close();
  63. }
  64. private void DisableMinimizeButton()
  65. {
  66. if (this.IsLoaded)
  67. {
  68. var helper = new WindowInteropHelper(this);
  69. var menu = GetSystemMenu(helper.Handle, false);
  70. if (menu != IntPtr.Zero)
  71. {
  72. DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND);
  73. }
  74. }
  75. }
  76. // Properties to bind to the XAML
  77. public double ZoomFactor
  78. {
  79. get => zoomFactor;
  80. set
  81. {
  82. zoomFactor = value;
  83. ApplyTransforms();
  84. }
  85. }
  86. public double RotationAngle
  87. {
  88. get => rotationAngle;
  89. set
  90. {
  91. rotationAngle = value;
  92. ApplyTransforms();
  93. }
  94. }
  95. // Zoom in button click event
  96. private void ZoomIn_Click(object sender, RoutedEventArgs e)
  97. {
  98. ZoomFactor *= 1.2; // Increase zoom factor by 20%
  99. }
  100. // Zoom out button click event
  101. private void ZoomOut_Click(object sender, RoutedEventArgs e)
  102. {
  103. ZoomFactor /= 1.2; // Decrease zoom factor by 20%
  104. }
  105. private void ZoomFit_Click(object sender, RoutedEventArgs e)
  106. {
  107. CalculateInitialZoomFactor();
  108. }
  109. // Rotate left button click event
  110. private void RotateLeft_Click(object sender, RoutedEventArgs e)
  111. {
  112. RotationAngle -= 90; // Rotate 90 degrees counterclockwise
  113. }
  114. // Rotate right button click event
  115. private void RotateRight_Click(object sender, RoutedEventArgs e)
  116. {
  117. RotationAngle += 90; // Rotate 90 degrees clockwise
  118. }
  119. // Apply the current zoom and rotation transforms to the image
  120. private void ApplyTransforms()
  121. {
  122. // Ensure the zoom factor stays within reasonable bounds
  123. if (ZoomFactor < 0.1) ZoomFactor = 0.1;
  124. if (ZoomFactor > 10.0) ZoomFactor = 10.0;
  125. // Create a ScaleTransform to apply the zoom
  126. ScaleTransform scaleTransform = new ScaleTransform(ZoomFactor, ZoomFactor);
  127. // Create a RotateTransform to apply the rotation
  128. RotateTransform rotateTransform = new RotateTransform(RotationAngle);
  129. // Combine the scale and rotation transforms
  130. TransformGroup transformGroup = new TransformGroup();
  131. transformGroup.Children.Add(scaleTransform);
  132. transformGroup.Children.Add(rotateTransform);
  133. // Apply the combined transform to the image
  134. imgViewer.LayoutTransform = transformGroup;
  135. // Center the image in the ScrollViewer
  136. CenterImageInScrollViewer();
  137. }
  138. // Center the image in the ScrollViewer
  139. private void CenterImageInScrollViewer()
  140. {
  141. if (scrollViewer != null && imgViewer != null)
  142. {
  143. // Calculate the center of the image
  144. double imageWidth = imgViewer.ActualWidth * ZoomFactor;
  145. double imageHeight = imgViewer.ActualHeight * ZoomFactor;
  146. // Calculate the center of the ScrollViewer
  147. double scrollViewerWidth = scrollViewer.ViewportWidth;
  148. double scrollViewerHeight = scrollViewer.ViewportHeight;
  149. // Calculate the horizontal and vertical offsets to center the image
  150. double horizontalOffset = (imageWidth - scrollViewerWidth) / 2;
  151. double verticalOffset = (imageHeight - scrollViewerHeight) / 2;
  152. // Set the scroll position to center the image
  153. scrollViewer.ScrollToHorizontalOffset(horizontalOffset > 0 ? horizontalOffset : 0);
  154. scrollViewer.ScrollToVerticalOffset(verticalOffset > 0 ? verticalOffset : 0);
  155. }
  156. }
  157. private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
  158. {
  159. if (Keyboard.Modifiers == ModifierKeys.Control)
  160. {
  161. if (e.Delta > 0)
  162. {
  163. zoomFactor *= 1.2; // Increase zoom factor by 20%
  164. }
  165. else
  166. {
  167. zoomFactor /= 1.2; // Decrease zoom factor by 20%
  168. }
  169. //e.Handled = true;
  170. }
  171. }
  172. private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
  173. {
  174. //按廉博要求,取消按control键
  175. //if (Keyboard.Modifiers == ModifierKeys.Control)
  176. //{
  177. if (e.Delta > 0)
  178. {
  179. ZoomFactor *= 1.1; // Zoom in
  180. }
  181. else
  182. {
  183. ZoomFactor /= 1.1; // Zoom out
  184. }
  185. // Optionally, center the zoom around the mouse cursor
  186. Point mousePosition = e.GetPosition(imgViewer);
  187. double prevZoomFactor = 1 / ZoomFactor;
  188. double offsetX = (mousePosition.X * ZoomFactor - mousePosition.X * prevZoomFactor) / prevZoomFactor;
  189. double offsetY = (mousePosition.Y * ZoomFactor - mousePosition.Y * prevZoomFactor) / prevZoomFactor;
  190. scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + offsetX);
  191. scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offsetY);
  192. //}
  193. }
  194. // 当用户按下鼠标左键时,记录按下的位置
  195. private void Image_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  196. {
  197. dragStartPoint = e.GetPosition(scrollViewer);
  198. }
  199. // 当用户拖动鼠标时,根据鼠标的移动调整 ScrollViewer 的滚动位置
  200. private void Image_PreviewMouseMove(object sender, MouseEventArgs e)
  201. {
  202. if (dragStartPoint.HasValue && e.LeftButton == MouseButtonState.Pressed)
  203. {
  204. Point currentPoint = e.GetPosition(scrollViewer);
  205. Vector delta = Point.Subtract(dragStartPoint.Value, currentPoint);
  206. // 调整 ScrollViewer 的滚动位置
  207. scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + delta.X);
  208. scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + delta.Y);
  209. // 更新拖动起点为当前点,以防止累积偏移
  210. dragStartPoint = currentPoint;
  211. }
  212. }
  213. // 当用户释放鼠标左键时,停止拖动
  214. private void Image_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  215. {
  216. dragStartPoint = null;
  217. }
  218. private void Window_Loaded(object sender, RoutedEventArgs e)
  219. {
  220. DisableMinimizeButton();
  221. if (imgViewer.Source != null)
  222. {
  223. CalculateInitialZoomFactor();
  224. }
  225. }
  226. // 计算初始缩放比例,使得图片正好适应 ScrollViewer 的视口
  227. private void CalculateInitialZoomFactor()
  228. {
  229. if (scrollViewer != null && imgViewer != null)
  230. {
  231. // 获取 ScrollViewer 的视口尺寸
  232. double scrollViewerWidth = scrollViewer.ViewportWidth;
  233. double scrollViewerHeight = scrollViewer.ViewportHeight;
  234. // 获取图片的实际尺寸
  235. double imageWidth = imgViewer.ActualWidth;
  236. double imageHeight = imgViewer.ActualHeight;
  237. // 如果图片还没有加载完成,等待加载完成后再次尝试计算
  238. if (double.IsNaN(imageWidth) || double.IsNaN(imageHeight))
  239. {
  240. imgViewer.Loaded += (s, ev) => CalculateInitialZoomFactor();
  241. return;
  242. }
  243. // 计算水平和垂直方向的缩放比例
  244. double scaleX = scrollViewerWidth / imageWidth;
  245. double scaleY = scrollViewerHeight / imageHeight;
  246. // 选择较小的缩放比例,以确保图片完全适应 ScrollViewer
  247. double initialZoomFactor = Math.Min(scaleX, scaleY);
  248. // 确保缩放比例不会小于 0.1 或大于 10.0
  249. if (initialZoomFactor < 0.1) initialZoomFactor = 0.1;
  250. if (initialZoomFactor > 10.0) initialZoomFactor = 10.0;
  251. // 应用初始缩放比例
  252. ZoomFactor = initialZoomFactor;
  253. // 中心化图片
  254. CenterImageInScrollViewer();
  255. }
  256. }
  257. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  258. {
  259. }
  260. private void Window_Closed(object sender, EventArgs e)
  261. {
  262. //WindowState = "Maximized"
  263. if (WindowState != WindowState.Maximized)
  264. {
  265. int width = (int)this.Width;
  266. int height = (int)this.Height;
  267. string dialogSize = $"{width},{height}";
  268. CfginiItem.GetConfigItem().DialogImageSize = dialogSize;
  269. }
  270. }
  271. private void ImageSave_Click(object sender, RoutedEventArgs e)
  272. {
  273. if(imgViewer.Source != null)
  274. {
  275. ExportImage(imgViewer);
  276. }
  277. }
  278. private void ExportImage(Image imageControl)
  279. {
  280. if (imageControl.Source is BitmapImage bitmapImage)
  281. {
  282. try
  283. {
  284. // 1. 获取原始文件路径
  285. string sourcePath = bitmapImage.UriSource.LocalPath;
  286. if (string.IsNullOrEmpty(sourcePath) || !File.Exists(sourcePath))
  287. {
  288. MessageBox.Show("无法确定原始文件路径或文件不存在。");
  289. return;
  290. }
  291. // 2. 弹出保存对话框
  292. VistaSaveFileDialog saveFileDialog = new VistaSaveFileDialog
  293. {
  294. FileName = Path.GetFileName(sourcePath), // 默认文件名
  295. DefaultExt = Path.GetExtension(sourcePath),
  296. Filter = $"Image Files (*{Path.GetExtension(sourcePath)})|*{Path.GetExtension(sourcePath)}|All Files (*.*)|*.*"
  297. };
  298. if (saveFileDialog.ShowDialog() == true)
  299. {
  300. // 3. 直接复制文件
  301. File.Copy(sourcePath, saveFileDialog.FileName, true);
  302. // MessageBox.Show("图片导出成功。");
  303. }
  304. }
  305. catch (Exception ex)
  306. {
  307. MessageBox.Show($"导出失败: {ex.Message}");
  308. }
  309. }
  310. else
  311. {
  312. MessageBox.Show("Image控件中没有有效的图像。");
  313. }
  314. }
  315. //////////////////////////////////////////////////////////
  316. }
  317. }