领胜LDS 键盘AOI检测项目
xcd
2020-06-24 d6c577e17ee7bb5331dd51d803f9b42441b0f0e5
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using Bro.Common.ImageCanvas.Shapes;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
 
namespace Bro.Common.ImageCanvas
{
    /// <summary>
    /// ImageCanvas.xaml 的交互逻辑
    /// </summary>
    public partial class ImageCanvas : System.Windows.Controls.UserControl
    {
        bool isFirstLoad = true;
 
        public ImageCanvas()
        {
            InitializeComponent();
            GotFocus += (sender, args) => Keyboard.Focus(ZoomAndPanControl);
 
            Loaded += (s, e) => { isFirstLoad = true; };
 
            _infoClearTimer = new System.Threading.Timer(ClearInfo, null, Timeout.Infinite, Timeout.Infinite);
 
            ZoomAndPanControl.OnMousePositionChanged += OnMousePointChanged;
        }
 
        #region ImageSource
        public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(Bitmap), typeof(ImageCanvas), new PropertyMetadata(new Bitmap(1000, 1000), OnImagSourcePropertyChanged));
 
        public Bitmap ImageSource
        {
            get => GetValue(ImageSourceProperty) as Bitmap;
            set => SetValue(ImageSourceProperty, value);
        }
 
        private static void OnImagSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageCanvas ctrl = d as ImageCanvas;
            if (ctrl != null)
            {
                Bitmap map = e.NewValue as Bitmap;
                if (map != null)
                {
                    ctrl.actualContent.Source = map.BitmapToWriteableBitmap();
 
                    if (ctrl.isFirstLoad)
                    {
                        ctrl.isFirstLoad = false;
 
                        Task.Run(() =>
                        {
                            Thread.Sleep(100);
                            ctrl.Dispatcher.Invoke(() =>
                            {
                                ctrl.ZoomAndPanControl.FitCommand?.Execute(null);
                            });
                        });
                    }
                }
            }
        }
        #endregion
 
        #region ToolBar
        public static readonly DependencyProperty IsShowToolBarProperty = DependencyProperty.Register("IsShowToolBar", typeof(bool), typeof(ImageCanvas), new PropertyMetadata(true, OnIsShowToolBarPropertyChanged));
 
        private static void OnIsShowToolBarPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageCanvas ctrl = d as ImageCanvas;
            if (ctrl != null)
            {
                bool isVisible = (bool)e.NewValue;
                ctrl.toolBarTray.Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
            }
        }
 
        public bool IsShowToolBar
        {
            get => (bool)GetValue(IsShowToolBarProperty);
            set => SetValue(IsShowToolBarProperty, value);
        }
 
        private void btnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
 
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Bitmap map = System.Drawing.Image.FromFile(ofd.FileName) as Bitmap;
                if (map != null)
                {
                    ImageSource = map;
                }
            }
        }
 
        private void btnSaveImage_Click(object sender, RoutedEventArgs e)
        {
            if (ImageSource == null)
                return;
 
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "JPG文件|*.jpg|BMP文件|*.bmp";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string filePath = sfd.FileName;
                if (filePath.EndsWith("bmp"))
                {
                    ImageSource.Save(filePath, System.Drawing.Imaging.ImageFormat.Bmp);
                }
                else
                {
                    ImageSource.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
 
                SetTxtInfo($"图片保存成功! {filePath}");
            }
        }
 
        private void btnSaveImageResults_Click(object sender, RoutedEventArgs e)
        {
 
        }
 
        private void btnFitScreen_Click(object sender, RoutedEventArgs e)
        {
            ZoomAndPanControl.FitCommand?.Execute(null);
        }
 
        private void btnOriginImage_Click(object sender, RoutedEventArgs e)
        {
            ZoomAndPanControl.FillCommand?.Execute(null);
        }
        #endregion
 
        #region Visula Shapes
        public static readonly DependencyProperty ShapeListProperty =
DependencyProperty.Register("ShapeList", typeof(ObservableCollection<ShapeElementBase>), typeof(ImageCanvas), new PropertyMetadata(new ObservableCollection<ShapeElementBase>(), ObservableCollectionChangedCallback));
 
        private static void ObservableCollectionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var canvas = (ImageCanvas)d;
            //canvas.visualCanvas.VisualList = new ObservableCollection<ShapeElementBase>(canvas.ShapeList);
            //canvas.ShapeList.CollectionChanged += canvas.Shapes_CollectionChanged;
 
            canvas.visualCanvas.VisualList = canvas.ShapeList;
        }
 
        public ObservableCollection<ShapeElementBase> ShapeList
        {
            get => GetValue(ShapeListProperty) as ObservableCollection<ShapeElementBase>;
            set
            {
                SetValue(ShapeListProperty, value);
 
                //value.CollectionChanged -= Shapes_CollectionChanged;
                //value.CollectionChanged += Shapes_CollectionChanged;
            }
        }
 
        //private void Shapes_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        //{
        //    //this.visualCanvas.VisualList = new ObservableCollection<ShapeElementBase>(this.ShapeList);
        //}
        #endregion
 
        #region txtInfo
        readonly System.Threading.Timer _infoClearTimer = null;
 
        private void ClearInfo(object state)
        {
            txtInfo.Dispatcher.Invoke(() =>
            {
                txtInfo.Text = "";
            });
        }
 
        private void SetTxtInfo(string info)
        {
            txtInfo.Text = info;
 
            if (!string.IsNullOrWhiteSpace(info))
            {
                _infoClearTimer.Change(5000, Timeout.Infinite);
            }
        }
        #endregion
 
        #region MousePoint
        private void OnMousePointChanged(System.Windows.Point point)
        {
            visualCanvas.OnMousePointChanged(point);
        }
        #endregion
    }
}