C3032
2025-12-20 15492363f898704b51afce5f1c88fa3b754cbabc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using OpenCvSharp;
using OpenCvSharp.WpfExtensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
 
namespace SmartScanner
{
    /// <summary>
    /// ImageDisplayWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ImageDisplayWindow : System.Windows.Window
    {
        private List<string> _imagePaths = new List<string>();
        private int _currentIndex = -1;
        public ImageDisplayWindow()
        {
            InitializeComponent();
        }
        public void AddImage(List<string> imagePath)
        {
            for (int i = 0; i < imagePath.Count; i++)
            {
                if (!File.Exists(imagePath[i]))
                {
                    MessageBox.Show($"图片不存在: {imagePath}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }
            // 添加到列表
            //_imagePaths = imagePath;
            _imagePaths = new List<string>(imagePath);
            ShowImage(0);
        }
 
        private void ShowImage(int index)
        {
            if (index < 0 || index >= _imagePaths.Count) return;
 
            _currentIndex = index;
            string imagePath = _imagePaths[index];
 
            try
            {
                // 使用OpenCvSharp读取图片
                using (Mat image = Cv2.ImRead(imagePath, ImreadModes.Color))
                {
                    if (image.Empty())
                    {
                        throw new Exception("图片加载失败");
                    }
 
                    // 使用Dispatcher确保UI线程更新
                    Dispatcher.Invoke(() =>
                    {
                        ResultImage.Source = image.ToBitmapSource();
                        Title = $"图片查看 ({index + 1}/{_imagePaths.Count}) - {System.IO.Path.GetFileName(imagePath)}";
 
                        // 更新按钮状态
                        PrevButton.IsEnabled = index > 0;
                        NextButton.IsEnabled = index < _imagePaths.Count - 1;
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载图片失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
 
        private void PrevButton_Click(object sender, RoutedEventArgs e)
        {
            ShowImage(_currentIndex - 1);
        }
 
        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            ShowImage(_currentIndex + 1);
        }
        public bool ShowImage_main(string imagePath)
        {
            try
            {
                // 使用OpenCvSharp读取图片
                using (Mat image = Cv2.ImRead(imagePath, ImreadModes.Color))
                {
                    if (image.Empty())
                    {
                        return false;
                    }
 
                    // 使用Dispatcher确保UI线程更新
                    Dispatcher.Invoke(() =>
                    {
                        ResultImage.Source = image.ToBitmapSource();
                        Title = $"图片查看:{System.IO.Path.GetFileName(imagePath)}";
 
                        // 更新按钮状态
                        PrevButton.IsEnabled = false;
                        NextButton.IsEnabled = false;
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载图片失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            return true;
        }
    }
}