image_text.xaml.cs 10.6 KB
using Acc.ImageBox;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
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.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SmartScan.SetControl.WPF
{
    /// <summary>
    /// image_text.xaml 的交互逻辑
    /// </summary>
    public partial class image_text : System.Windows.Controls.UserControl
    {
        public AccImageBox imageBox;

        public image_text()
        {
            InitializeComponent();
            // 初始化 imageBox
            imageBox = new AccImageBox();
            imageBox.Dock = DockStyle.Fill;
            imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Zoom;

            // 将 imageBox 添加到 WindowsFormsHost
            imageViewer.Child = imageBox;
            // 确保画布已初始化并且透明
            if (centerPointsCanvas == null)
            {
                centerPointsCanvas = new Canvas();
                centerPointsCanvas.Background = System.Windows.Media.Brushes.Transparent;
                centerPointsCanvas.IsHitTestVisible = false;
            }

            Console.WriteLine("image_text 控件已初始化,画布已设置");
        }
        /// <summary>
        /// 加载图像
        /// </summary>
        /// <param name="imagePath"></param>
        public void LoadImage(string imagePath)
        {
            if (File.Exists(imagePath))
            {
                try
                {
                    if (imageBox.Image != null)
                    {
                        imageBox.Image.Dispose();
                    }

                    imageBox.Image = System.Drawing.Image.FromFile(imagePath);
                    FitToScreen(); // 自动适应图像大小
                }
                catch (Exception ex)
                {
                    // 处理错误
                }
            }
        }

        // 公共方法以适应屏幕 - 使其从 WinForms 可访问
        public void FitToScreen()
        {
            if (imageBox?.Image != null)
            {
                imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Zoom;
                imageBox.ZoomToFit();
                imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Normal;

                double currentZoom = imageBox.ZoomFactor;
                imageBox.Zoom = (int)(currentZoom * 100);
            }
        }
        // 清空中心点方法
        public void ClearCenterPoints()
        {
            centerPointsCanvas.Children.Clear();
        }

        // 添加中心点方法
        public void AddCenterPoint(double x, double y)
        {
            // 创建一个表示中心点的椭圆
            Ellipse centerPoint = new Ellipse
            {
                Width = 20,
                Height = 20,
                Fill = System.Windows.Media.Brushes.Red,
                Stroke = System.Windows.Media.Brushes.White,
                StrokeThickness = 2
            };

            // 设置椭圆位置,使其中心在指定坐标
            Canvas.SetLeft(centerPoint, x - 5);
            Canvas.SetTop(centerPoint, y - 5);

            // 添加到画布
            centerPointsCanvas.Children.Add(centerPoint);

            // 可选:添加十字线或其他标记以更清晰地标识中心点
            Line horizontalLine = new Line
            {
                X1 = x - 10,
                Y1 = y,
                X2 = x + 10,
                Y2 = y,
                Stroke = System.Windows.Media.Brushes.White,
                StrokeThickness = 1
            };

            Line verticalLine = new Line
            {
                X1 = x,
                Y1 = y - 10,
                X2 = x,
                Y2 = y + 10,
                Stroke = System.Windows.Media.Brushes.White,
                StrokeThickness = 1
            };

            centerPointsCanvas.Children.Add(horizontalLine);
            centerPointsCanvas.Children.Add(verticalLine);
        }

        // 在 image_text 类中添加
        public void DrawCenterPointsOnImage(System.Drawing.PointF[] centers)
        {
            if (imageBox == null || imageBox.Image == null || centers == null || centers.Length == 0)
                return;

            // 存储中心点数据
            _centers = centers;

            // 创建一个事件处理程序来自定义绘制
            imageBox.Paint -= ImageBox_Paint;
            imageBox.Paint += ImageBox_Paint;
            // 强制重绘
            imageBox.Invalidate();
        }

        private System.Drawing.PointF[] _centers;

        // 自定义绘制事件处理程序
        private void ImageBox_Paint(object sender, PaintEventArgs e)
        {
            if (_centers == null || _centers.Length == 0 || imageBox == null || imageBox.Image == null)
                return;

            return;
            // 获取图像和显示的关系
            double zoomFactor = imageBox.ZoomFactor;
            System.Drawing.Rectangle viewport = imageBox.GetImageViewPort();
            // 设置高质量绘制
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            //int circleRadius = 10; // 原来是15,这里调大到20
                                   // 基础尺寸
            float baseCircleRadius = 15;
            // 平衡缩放 - 使用平方根或对数来减缓缩放效果
            float adjustedZoomFactor = (float)Math.Sqrt(zoomFactor); // 或使用 Math.Log10(zoomFactor * 10)

            // 计算圆半径 (平衡缩放效果)
            float circleRadius = baseCircleRadius * adjustedZoomFactor;

            // 设置合理的限制
            circleRadius = Math.Max(10, Math.Min(circleRadius, 40));

            // 计算字体大小
            float fontSize = 12 * adjustedZoomFactor;
            fontSize = Math.Max(8, Math.Min(fontSize, 20));
            // 准备绘制工具
            using (System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Transparent, 1))
            using (System.Drawing.SolidBrush fillBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Transparent))
            using (System.Drawing.SolidBrush textBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black))
            using (System.Drawing.Font font = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold))
            {
                for (int i = 0; i < _centers.Length; i++)
                {
                    // 计算屏幕坐标
                    float screenX = (_centers[i].X * (float)zoomFactor) + viewport.X;
                    float screenY = (_centers[i].Y * (float)zoomFactor) + viewport.Y;

                    // 绘制圆形背景\
                    float diameter = circleRadius * 2;
                    e.Graphics.FillEllipse(fillBrush, screenX - circleRadius, screenY - circleRadius, diameter, diameter);
                    e.Graphics.DrawEllipse(pen, screenX - circleRadius, screenY - circleRadius, diameter, diameter);

                    // 绘制数字标记
                    string number = (i + 1).ToString();
                    System.Drawing.SizeF textSize = e.Graphics.MeasureString(number, font);

                    // 计算文本位置使其居中
                    float textX = screenX - (textSize.Width / 2);
                    float textY = screenY - (textSize.Height / 2);

                    e.Graphics.DrawString(number, font, textBrush, textX, textY);

                    Console.WriteLine($"绘制数字标记 {i + 1} 在位置 ({screenX}, {screenY})");
                }
            }
        }



        #region 按钮事件
        /// <summary>
        /// 放大按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZoomInButton_Click(object sender, RoutedEventArgs e)
        {
            if (imageBox?.Image != null)
            {
                // 减小缩放比例,减少20%
                double currentZoom = imageBox.ZoomFactor;
                double newZoom = currentZoom * 1.2;
                imageBox.Zoom = (int)(newZoom * 100);  // 假设Zoom属性是百分比形式
            }
        }
        // 缩小按钮事件处理程序
        private void ZoomOutButton_Click(object sender, RoutedEventArgs e)
        {
            if (imageBox?.Image != null)
            {
                // 减小缩放比例,减少20%
                double currentZoom = imageBox.ZoomFactor;
                double newZoom = currentZoom * 0.8;
                imageBox.Zoom = (int)(newZoom * 100);  // 假设Zoom属性是百分比形式
            }
        }
        // 适应屏幕按钮事件处理程序
        private void FitToScreenButton_Click(object sender, RoutedEventArgs e)
        {
            if (imageBox?.Image != null)
            {
                // 使用SizeMode属性设置图像适应控件
                imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Zoom;
                imageBox.ZoomToFit();
                // 然后切换回Normal模式,以便后续手动缩放
                imageBox.SizeMode = (ImageBoxSizeMode)PictureBoxSizeMode.Normal;

                // 保存当前缩放值,以便在Normal模式下保持相同的显示大小
                double currentZoom = imageBox.ZoomFactor;
                imageBox.Zoom = (int)(currentZoom * 100);
            }
        }
        // 旋转按钮事件处理程序
        private void RotateButton_Click(object sender, RoutedEventArgs e)
        {
            if (imageBox?.Image != null)
            {
                try
                {
                    // 创建一个新的临时图像来旋转
                    Bitmap originalImage = new Bitmap(imageBox.Image);
                    originalImage.RotateFlip(RotateFlipType.Rotate90FlipNone);

                    // 替换当前图像
                    System.Drawing.Image oldImage = imageBox.Image;
                    imageBox.Image = originalImage;

                    // 释放旧图像
                    oldImage.Dispose();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show($"旋转图片时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
        #endregion 
    }
}