using Ookii.Dialogs.Wpf; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; namespace MV485.Dlg { /// /// DlgImage.xaml 的交互逻辑 /// public partial class DlgImage : Window { [DllImport("user32.dll")] private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport("user32.dll")] private static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags); private const uint SC_MINIMIZE = 0xF020; private const uint MF_BYCOMMAND = 0x00000000; private double zoomFactor = 1.0; private double rotationAngle = 0; private Point? dragStartPoint; // 用于记录鼠标按下的位置 private string _imageSourcePath; public string ImageSourcePath => _imageSourcePath; public DlgImage(string imageSourcePath) { InitializeComponent(); //int width = CfginiItem.GetConfigItem().DialogImageSize_Width; //int height = CfginiItem.GetConfigItem().DialogImageSize_Height; //if (width != 0 && height != 0) //{ // this.Width = width; // this.Height = height; //} _imageSourcePath = imageSourcePath; if (File.Exists(imageSourcePath)) { imgViewer.Source = new BitmapImage(new Uri(imageSourcePath, UriKind.RelativeOrAbsolute)); } DataContext = this; } // 鼠标点击外部时关闭窗口 private void OnMouseDownOutside(object sender, MouseButtonEventArgs e) { //this.Close(); } private void ImgWindow_Deactivated(object sender, EventArgs e) { //throw new NotImplementedException(); //this.Close(); } private void DisableMinimizeButton() { if (this.IsLoaded) { var helper = new WindowInteropHelper(this); var menu = GetSystemMenu(helper.Handle, false); if (menu != IntPtr.Zero) { DeleteMenu(menu, SC_MINIMIZE, MF_BYCOMMAND); } } } public double ZoomFactor { get => zoomFactor; set { zoomFactor = value; ApplyTransforms(); } } public double RotationAngle { get => rotationAngle; set { rotationAngle = value; ApplyTransforms(); } } private void ZoomIn_Click(object sender, RoutedEventArgs e) { ZoomFactor *= 1.2; // Increase zoom factor by 20% } // Zoom out button click event private void ZoomOut_Click(object sender, RoutedEventArgs e) { ZoomFactor /= 1.2; // Decrease zoom factor by 20% } private void ZoomFit_Click(object sender, RoutedEventArgs e) { CalculateInitialZoomFactor(); } private void RotateLeft_Click(object sender, RoutedEventArgs e) { RotationAngle -= 90; // Rotate 90 degrees counterclockwise } // Rotate right button click event private void RotateRight_Click(object sender, RoutedEventArgs e) { RotationAngle += 90; // Rotate 90 degrees clockwise } private void ApplyTransforms() { // Ensure the zoom factor stays within reasonable bounds if (ZoomFactor < 0.1) ZoomFactor = 0.1; if (ZoomFactor > 10.0) ZoomFactor = 10.0; // Create a ScaleTransform to apply the zoom ScaleTransform scaleTransform = new ScaleTransform(ZoomFactor, ZoomFactor); // Create a RotateTransform to apply the rotation RotateTransform rotateTransform = new RotateTransform(RotationAngle); // Combine the scale and rotation transforms TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(scaleTransform); transformGroup.Children.Add(rotateTransform); // Apply the combined transform to the image imgViewer.LayoutTransform = transformGroup; // Center the image in the ScrollViewer CenterImageInScrollViewer(); } // Center the image in the ScrollViewer private void CenterImageInScrollViewer() { if (scrollViewer != null && imgViewer != null) { // Calculate the center of the image double imageWidth = imgViewer.ActualWidth * ZoomFactor; double imageHeight = imgViewer.ActualHeight * ZoomFactor; // Calculate the center of the ScrollViewer double scrollViewerWidth = scrollViewer.ViewportWidth; double scrollViewerHeight = scrollViewer.ViewportHeight; // Calculate the horizontal and vertical offsets to center the image double horizontalOffset = (imageWidth - scrollViewerWidth) / 2; double verticalOffset = (imageHeight - scrollViewerHeight) / 2; // Set the scroll position to center the image scrollViewer.ScrollToHorizontalOffset(horizontalOffset > 0 ? horizontalOffset : 0); scrollViewer.ScrollToVerticalOffset(verticalOffset > 0 ? verticalOffset : 0); } } private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { if (Keyboard.Modifiers == ModifierKeys.Control) { if (e.Delta > 0) { zoomFactor *= 1.2; // Increase zoom factor by 20% } else { zoomFactor /= 1.2; // Decrease zoom factor by 20% } //e.Handled = true; } } private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { //按廉博要求,取消按control键 //if (Keyboard.Modifiers == ModifierKeys.Control) //{ if (e.Delta > 0) { ZoomFactor *= 1.1; // Zoom in } else { ZoomFactor /= 1.1; // Zoom out } // Optionally, center the zoom around the mouse cursor Point mousePosition = e.GetPosition(imgViewer); double prevZoomFactor = 1 / ZoomFactor; double offsetX = (mousePosition.X * ZoomFactor - mousePosition.X * prevZoomFactor) / prevZoomFactor; double offsetY = (mousePosition.Y * ZoomFactor - mousePosition.Y * prevZoomFactor) / prevZoomFactor; scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + offsetX); scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offsetY); //} } // 当用户按下鼠标左键时,记录按下的位置 private void Image_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { dragStartPoint = e.GetPosition(scrollViewer); } // 当用户拖动鼠标时,根据鼠标的移动调整 ScrollViewer 的滚动位置 private void Image_PreviewMouseMove(object sender, MouseEventArgs e) { if (dragStartPoint.HasValue && e.LeftButton == MouseButtonState.Pressed) { Point currentPoint = e.GetPosition(scrollViewer); Vector delta = Point.Subtract(dragStartPoint.Value, currentPoint); // 调整 ScrollViewer 的滚动位置 scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + delta.X); scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + delta.Y); // 更新拖动起点为当前点,以防止累积偏移 dragStartPoint = currentPoint; } } // 当用户释放鼠标左键时,停止拖动 private void Image_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { dragStartPoint = null; } private void Window_Loaded(object sender, RoutedEventArgs e) { DisableMinimizeButton(); if (imgViewer.Source != null) { CalculateInitialZoomFactor(); } } // 计算初始缩放比例,使得图片正好适应 ScrollViewer 的视口 private void CalculateInitialZoomFactor() { if (scrollViewer != null && imgViewer != null) { // 获取 ScrollViewer 的视口尺寸 double scrollViewerWidth = scrollViewer.ViewportWidth; double scrollViewerHeight = scrollViewer.ViewportHeight; // 获取图片的实际尺寸 double imageWidth = imgViewer.ActualWidth; double imageHeight = imgViewer.ActualHeight; // 如果图片还没有加载完成,等待加载完成后再次尝试计算 if (double.IsNaN(imageWidth) || double.IsNaN(imageHeight)) { imgViewer.Loaded += (s, ev) => CalculateInitialZoomFactor(); return; } // 计算水平和垂直方向的缩放比例 double scaleX = scrollViewerWidth / imageWidth; double scaleY = scrollViewerHeight / imageHeight; // 选择较小的缩放比例,以确保图片完全适应 ScrollViewer double initialZoomFactor = Math.Min(scaleX, scaleY); // 确保缩放比例不会小于 0.1 或大于 10.0 if (initialZoomFactor < 0.1) initialZoomFactor = 0.1; if (initialZoomFactor > 10.0) initialZoomFactor = 10.0; // 应用初始缩放比例 ZoomFactor = initialZoomFactor; // 中心化图片 CenterImageInScrollViewer(); } } private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { } private void Window_Closed(object sender, EventArgs e) { //WindowState = "Maximized" if (WindowState != WindowState.Maximized) { int width = (int)this.Width; int height = (int)this.Height; string dialogSize = $"{width},{height}"; //CfginiItem.GetConfigItem().DialogImageSize = dialogSize; } } private void ImageSave_Click(object sender, RoutedEventArgs e) { if (imgViewer.Source != null) { ExportImage(imgViewer); } } private void ExportImage(Image imageControl) { if (imageControl.Source is BitmapImage bitmapImage) { try { // 1. 获取原始文件路径 string sourcePath = bitmapImage.UriSource.LocalPath; if (string.IsNullOrEmpty(sourcePath) || !File.Exists(sourcePath)) { MessageBox.Show("无法确定原始文件路径或文件不存在。"); return; } // 2. 弹出保存对话框 VistaSaveFileDialog saveFileDialog = new VistaSaveFileDialog { FileName = System.IO.Path.GetFileName(sourcePath), // 默认文件名 DefaultExt = System.IO.Path.GetExtension(sourcePath), Filter = $"Image Files (*{System.IO.Path.GetExtension(sourcePath)})|*{System.IO.Path.GetExtension(sourcePath)}|All Files (*.*)|*.*" }; if (saveFileDialog.ShowDialog() == true) { // 3. 直接复制文件 File.Copy(sourcePath, saveFileDialog.FileName, true); // MessageBox.Show("图片导出成功。"); } } catch (Exception ex) { MessageBox.Show($"导出失败: {ex.Message}"); } } else { MessageBox.Show("Image控件中没有有效的图像。"); } } } }