lixiaojun
2024-08-15 11bfb11f3f352315fe64104509adda13d9f870d0
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Eventech.DynPicture.Model;
using System.Threading;
 
namespace Eventech.DynPicture.WinFrmUI
{
    /// <summary>
    /// 拓展绘制文本,选中文本
    /// </summary>
    public partial class PictureEditInfo : PictureEditBasic
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public PictureEditInfo()
        {
            InitializeComponent();
        }
 
        #region 事件
 
        /// <summary>
        /// 图片信息上按下鼠标时触发
        /// </summary>
        public event Action<IPictureBaseInfo, MouseEventArgs> ImageInfoMouseDownEvent;
 
        /// <summary>
        /// 图片信息点击时触发
        /// </summary>
        public event Action<IPictureBaseInfo, MouseEventArgs> ImageInfoMouseClickEvent;
 
        /// <summary>
        /// 图片信息双击时触发
        /// </summary>
        public event Action<IPictureBaseInfo, MouseEventArgs> ImageInfoMouseDoubleClickEvent;
 
        /// <summary>
        /// 更新图片信息事件
        /// </summary>
        public event Action<IPictureBaseInfo> UpdateImageInfoEvent;
 
        #endregion
 
        /// <summary>
        /// 当前选中的图片信息
        /// </summary>
        public IPictureBaseInfo SelectedImageInfo
        {
            get { return _selectedImageInfo; }
            private set{_selectedImageInfo = value;}
        }
        private IPictureBaseInfo _selectedImageInfo;
 
        /// <summary>
        /// 是否高亮显示图片信息对象
        /// </summary>
        public bool IsHighlightImageInfo
        {
            get { return _isHighlightImageInfo; }
            set { _isHighlightImageInfo = value; }
        }
        private bool _isHighlightImageInfo = false;
 
        /// <summary>
        /// 选中图片信息时的鼠标信息
        /// </summary>
        protected MouseEventArgs _imageSelectedMouse;
 
        /// <summary>
        /// 是否显示图片信息选中外观
        /// </summary>
        public bool ShowSelectedAppearance
        {
            get { return _showSelectedAppearance; }
            set { _showSelectedAppearance = value; }
        }
        private bool _showSelectedAppearance = true;
 
        /// <summary>
        /// 是否允许鼠标按下移动图片信息
        /// </summary>
        public bool AllowMouseMoveImageInfo
        {
            get { return _allowMouseMoveImageInfo; }
            set { _allowMouseMoveImageInfo = value; }
        }
        private bool _allowMouseMoveImageInfo = false;
 
        /// <summary>
        /// 是否允许键盘方向键移动
        /// </summary>
        public bool AllowKeyDirectionMove
        {
            get { return _allowKeyDirectionMove; }
            set { _allowKeyDirectionMove = value; }
        }
        private bool _allowKeyDirectionMove = false;
 
        /// <summary>
        /// 初始化显示图片
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        protected override Image CreateOriginDisplayImage(Image img)
        {
            var cmg = base.CreateOriginDisplayImage(img);
            if (cmg == null)
                return null;
            _selectedImageInfo = null;
            DrawImageInfos(cmg, _imageInfos);
            return cmg;
        }
 
        /// <summary>
        /// 图片信息
        /// </summary>
        public virtual List<IPictureBaseInfo> ImageInfos
        {
            get { return _imageInfos; }
            set
            {
                _imageInfos = value;
                this.DisplayImage = _img;
            }
        }
        private List<IPictureBaseInfo> _imageInfos = null;
 
 
        /// <summary>
        /// 图片信息与坐标对应关系
        /// </summary>
        protected Dictionary<IPictureBaseInfo, ImageInfoCoordinate> _imageInfoDics = new Dictionary<IPictureBaseInfo,ImageInfoCoordinate>();
 
        /// <summary>
        /// 绘制图片信息信息(用Text绘制)
        /// </summary>
        /// <param name="img"></param>
        /// <param name="imageInfos"></param>
        protected void DrawImageInfos(Image img, List<IPictureBaseInfo> imageInfos)
        {
            if (_imageInfoDics != null && _imageInfoDics.Count > 0)
                _imageInfoDics.Clear();
            if (img == null)
                return;
            if (imageInfos == null || imageInfos.Count < 1)
                return;
            using (var g = Graphics.FromImage(img))
            {
                var levels = imageInfos.OrderBy(x => x.Level).Select(x => x.Level).Distinct().ToList();
                levels.ForEach(x => {
                    var levInfos = imageInfos.Where(t => t.Level == x).ToList();
                    levInfos.ForEach(y => {
                        var coor = y.GetCoordinate(g);
                        if (coor != null)
                        {
                            _imageInfoDics.Add(y,coor);
                            y.DrawInfo2(g,coor);
                        }
                    });
                });
            }
        }
 
        #region 移动图片信息
 
        //鼠标左键是否按下图片信息
        private bool _mouseLeftInfoDown = false;
        //鼠标按下对应图片上的位置
        private PointF _mouseLeftDownImagePoint;
        //鼠标左键移动点位置
        private Point _mouseLeftMovePoint;
        //是否移动了图片信息
        private bool _isImageInfoMove = false;
        /// <summary>
        /// 鼠标按下选中图片信息并触发ImageTextMouseDownEvent
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            SelImageInfo(e);
            var allowMoveImage = this.AllowMouseDownMoveImage;
            if (_selectedImageInfo != null)
            {
                if (this.AllowMouseMoveImageInfo)
                {
                    this.AllowMouseDownMoveImage = false;
                    _mouseLeftInfoDown = true;
                    _mouseLeftDownImagePoint = ViewPointToImage(e.Location).Value;
                    _mouseLeftMovePoint = e.Location;
                }
            }
            base.OnMouseDown(e);
            this.AllowMouseDownMoveImage = allowMoveImage;
        }
 
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (_mouseLeftInfoDown)
            {
                if (_desRectf.Contains(e.Location))
                {
                    _mouseLeftMovePoint = e.Location;
                    _isImageInfoMove = true;
                    this.Invalidate();
                }
            }
        }
 
        /// <summary>
        /// 绘制移动图片信息移动边框
        /// </summary>
        /// <param name="g"></param>
        protected virtual void DrawInfoMoveBounds(Graphics g)
        {
            if (_selectedImageInfo == null)
                return;
            if (!_mouseLeftInfoDown)
                return;
            if (!_isImageInfoMove)
                return;
            var mp = ViewPointToImage(_mouseLeftMovePoint);
            var x_add = mp.Value.X - _mouseLeftDownImagePoint.X;
            var y_add = mp.Value.Y - _mouseLeftDownImagePoint.Y;
 
            var coordinate = _imageInfoDics[_selectedImageInfo];
            var points = new PointF[5];
            points[0] = GetMoveZoomPointF(coordinate.AfterLeftTop, x_add, y_add);
            points[1] = GetMoveZoomPointF(coordinate.AfterRightTop, x_add, y_add);
            points[2] = GetMoveZoomPointF(coordinate.AfterRightBottom, x_add, y_add);
            points[3] = GetMoveZoomPointF(coordinate.AfterLeftBottom, x_add, y_add);
            points[4] = points[0];
            using (var pen = new Pen(Color.Blue, 2f))
            {
                g.DrawLines(pen, points);
            }
        }
 
        private PointF GetMoveZoomPointF(PointF P, float x, float y)
        {
            var sp = new PointF(P.X + x, P.Y + y);
            return SourcePointToZoom(sp);
        }
 
        /// <summary>
        /// 鼠标弹起
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            _mouseLeftInfoDown = false;
            if (_isImageInfoMove)
            {
                if (_selectedImageInfo != null)
                {
                    var loc = _desRectf.Contains(e.Location) ? e.Location : _mouseLeftMovePoint;
                    var mp = ViewPointToImage(loc);
                    var x_add = mp.Value.X - _mouseLeftDownImagePoint.X;
                    var y_add = mp.Value.Y - _mouseLeftDownImagePoint.Y;
 
                    if (_selectedImageInfo is PictureTextInfo)
                    {
                        var textInfo = _selectedImageInfo as PictureTextInfo;
                        textInfo.Point = new PointF(textInfo.Point.X + x_add, textInfo.Point.Y + y_add);
                    }
                    else if (_selectedImageInfo is PictureImageInfo)
                    {
                        var imageInfo = _selectedImageInfo as PictureImageInfo;
                        imageInfo.Point = new PointF(imageInfo.Point.X + x_add, imageInfo.Point.Y + y_add);
                    }
                    else if (_selectedImageInfo is PictureLineInfo)
                    {
                        var lineInfo = _selectedImageInfo as PictureLineInfo;
                        lineInfo.StartPointF = new PointF(lineInfo.StartPointF.X + x_add, lineInfo.StartPointF.Y + y_add);
                        lineInfo.EndPointF = new PointF(lineInfo.EndPointF.X + x_add, lineInfo.EndPointF.Y + y_add);
                    }
                    else if (_selectedImageInfo is PictureArrowInfo)
                    {
                        var arrowInfo = _selectedImageInfo as PictureArrowInfo;
                        arrowInfo.StartPointF = new PointF(arrowInfo.StartPointF.X + x_add, arrowInfo.StartPointF.Y + y_add);
                        arrowInfo.EndPointF = new PointF(arrowInfo.EndPointF.X + x_add, arrowInfo.EndPointF.Y + y_add);
                    }
                    else if (_selectedImageInfo is PictureMarkInfo)
                    {
                        var markInfo = _selectedImageInfo as PictureMarkInfo;
                        markInfo.StartPointF = new PointF(markInfo.StartPointF.X + x_add, markInfo.StartPointF.Y + y_add);
                        markInfo.EndPointF = new PointF(markInfo.EndPointF.X + x_add, markInfo.EndPointF.Y + y_add);
                        markInfo.ExtendPointF = new PointF(markInfo.ExtendPointF.X + x_add, markInfo.ExtendPointF.Y + y_add);
                    }
                    this.RedrawImageTextInfos();
                    if (this.UpdateImageInfoEvent != null)
                        this.UpdateImageInfoEvent(_selectedImageInfo);
                }
            }
            _isImageInfoMove = false;
        }
 
        #endregion
 
        /// <summary>
        /// 选择图片信息
        /// </summary>
        /// <param name="e"></param>
        protected virtual void SelImageInfo(MouseEventArgs e)
        {
            var sel = _selectedImageInfo;
            _selectedImageInfo = null;
            if (_displayImage != null)
            {
                if (_imageInfoDics != null && _imageInfoDics.Count > 0)
                {
                    if (_desRectf.Contains(e.Location))
                    {
                        var dp = ViewPointToDisplay(e.Location);
                        var sp = DisplayPointToSource(dp);
                        foreach (var x in _imageInfoDics)
                        {
                            var np = x.Value.Origin.RotatePointF(sp, x.Value.Angle, false);
                            if (x.Value.BeforeRectf.Contains(np))
                            {
                                _selectedImageInfo = x.Key;
                                _imageSelectedMouse = e;
                                if (this.ImageInfoMouseDownEvent != null)
                                    this.ImageInfoMouseDownEvent(x.Key, e);
                                break;
                            }
                        }
                    }
                }
            }
            this.Invalidate();
        }
 
        /// <summary>
        /// 触发ImageInfoMouseClickEvent
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (_selectedImageInfo != null)
            {
                if (this.ImageInfoMouseClickEvent != null)
                    this.ImageInfoMouseClickEvent(_selectedImageInfo, _imageSelectedMouse);
            }
        }
 
        /// <summary>
        /// 触发ImageInfoMouseDoubleClickEvent
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            if (_selectedImageInfo != null)
            {
                if (this.ImageInfoMouseDoubleClickEvent != null)
                    this.ImageInfoMouseDoubleClickEvent(_selectedImageInfo, _imageSelectedMouse);
            }
        }
 
        /// <summary>
        /// 绘制选中的图片信息的边框
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawSelTextBounds(e.Graphics);
            DrawImageTextBounds(e.Graphics);
            DrawInfoMoveBounds(e.Graphics);
        }
 
        /// <summary>
        /// 绘制选中文本边框
        /// </summary>
        /// <param name="g"></param>
        protected virtual void DrawSelTextBounds(Graphics g)
        {
            if (_showSelectedAppearance)
            {
                if (_selectedImageInfo != null)
                {
                    var coordinate = _imageInfoDics[_selectedImageInfo];
                    var points = new PointF[5];
                    points[0] = SourcePointToZoom(coordinate.AfterLeftTop);
                    points[1] = SourcePointToZoom(coordinate.AfterRightTop);
                    points[2] = SourcePointToZoom(coordinate.AfterRightBottom);
                    points[3] = SourcePointToZoom(coordinate.AfterLeftBottom);
                    points[4] = points[0];
                    using (var pen = new Pen(Color.Red, 1f))
                    {
                        g.DrawLines(pen, points);
                    }
                }
            }
        }
 
        /// <summary>
        /// 绘制文本边框
        /// </summary>
        /// <param name="g"></param>
        protected virtual void DrawImageTextBounds(Graphics g)
        {
            if (this.IsHighlightImageInfo)
            {
                if (this.ImageInfos != null && this.ImageInfos.Count > 0)
                {
                    if (_imageInfoDics != null && _imageInfoDics.Count > 0)
                    {
                        using (var pen = new Pen(Color.Blue, 2f))
                        {
                            _imageInfoDics.ToList().ForEach(x =>
                            {
                                if (x.Key != _selectedImageInfo)
                                {
                                    var coordinate = _imageInfoDics[x.Key];
                                    var points = new PointF[5];
                                    points[0] = SourcePointToZoom(coordinate.AfterLeftTop);
                                    points[1] = SourcePointToZoom(coordinate.AfterRightTop);
                                    points[2] = SourcePointToZoom(coordinate.AfterRightBottom);
                                    points[3] = SourcePointToZoom(coordinate.AfterLeftBottom);
                                    points[4] = points[0];
                                    g.DrawLines(pen, points);
                                }
                            });
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// 替换图片(如果尺寸相同保持状态,否则重置)
        /// </summary>
        /// <param name="img"></param>
        public override void ReplaceImage(Image img)
        {
            if (_img != null)
            {
                if (_img.Size == img.Size)
                {
                    _img = img;
                    var cimg = img.CloneC();
                    DrawImageInfos(cimg, _imageInfos);
                    RotateImage(cimg);
                    if (_displayImage != null)
                        _displayImage.Dispose();
                    _displayImage = cimg;
                    this.Invalidate();
                    return;
                }
            }
            this.Image = img;
        }
 
        /// <summary>
        /// 重绘图片信息信息
        /// </summary>
        /// <param name="imageTextInfos"></param>
        public virtual void RedrawImageTextInfos(List<IPictureBaseInfo> imageTextInfos)
        {
            _imageInfos = imageTextInfos;
            RedrawImageTextInfos();
            this.Invalidate();
        }
 
        /// <summary>
        /// 重绘显示文本
        /// </summary>
        /// <param name="id"></param>
        /// <param name="text"></param>
        public virtual void RedrawImageText(long  id, string text)
        {
            if (_imageInfos == null)
                return;
            var imageInfo = _imageInfos.Find(x=>x.ID==id&&x.InfoType==ePictureInfoType.Text);
            if (imageInfo == null)
                return;
            var imageText = imageInfo as PictureTextInfo;
            imageText.Text = text;
            RedrawImageTextInfos();
            this.Invalidate();
        }
 
 
 
        /// <summary>
        /// 重绘图片信息信息
        /// </summary>
        public virtual void RedrawImageTextInfos()
        {
            if (_img == null)
                return;
            //SpinWait.SpinUntil(() => !_isAsyncLoading);
            var cimg = _img.CloneC();
            DrawImageInfos(cimg, _imageInfos);
            RotateImage(cimg);
            if (_displayImage != null)
                _displayImage.Dispose();
            _displayImage = cimg;
            if (_selectedImageInfo != null)
            {
                if (_imageInfos == null || _imageInfos.Count < 1)
                    _selectedImageInfo = null;
                else
                    _selectedImageInfo = _imageInfos.Find(x => x.ID == _selectedImageInfo.ID);
            }
            this.Invalidate();
        }
 
 
       
 
        public void SetSelImageInfo(long id)
        {
            if (this.ImageInfos == null)
                return;
            var imageInfo = this.ImageInfos.Find(x => x.ID == id);
            if (imageInfo == null)
                return;
            _selectedImageInfo = imageInfo;
            this.Invalidate();
        }
 
        /// <summary>
        /// 响应键盘事件(上下左右)
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (this.AllowKeyDirectionMove)
            {
                if (this.SelectedImageInfo != null)
                {
                    var rotateKey = TransferRotateKey(keyData);
                    switch (rotateKey)
                    {
                        case Keys.Up:
                            this.SelectedImageInfo.IncreaseAxisValue(eAxis.Y,-1);
                            this.RedrawImageTextInfos();
                            if (this.UpdateImageInfoEvent != null)
                                this.UpdateImageInfoEvent(_selectedImageInfo);
                            return true;
                        case Keys.Down:
                            this.SelectedImageInfo.IncreaseAxisValue(eAxis.Y, 1);
                            this.RedrawImageTextInfos();
                            if (this.UpdateImageInfoEvent != null)
                                this.UpdateImageInfoEvent(_selectedImageInfo);
                            return true;
                        case Keys.Left:
                            this.SelectedImageInfo.IncreaseAxisValue(eAxis.X, -1);
                            this.RedrawImageTextInfos();
                            if (this.UpdateImageInfoEvent != null)
                                this.UpdateImageInfoEvent(_selectedImageInfo);
                            return true;
                        case Keys.Right:
                            this.SelectedImageInfo.IncreaseAxisValue(eAxis.X, 1);
                            this.RedrawImageTextInfos();
                            if (this.UpdateImageInfoEvent != null)
                                this.UpdateImageInfoEvent(_selectedImageInfo);
                            return true;
                        default: break;
                    }
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
 
        /// <summary>
        /// 根据图片Rotate角度转化上下左右键盘
        /// </summary>
        /// <param name="directionKey">方向键</param>
        /// <returns>Rotate后的方向键</returns>
        protected virtual Keys TransferRotateKey(Keys directionKey)
        {
            var direcKeys = new List<Keys>() { Keys.Left, Keys.Up, Keys.Right, Keys.Down };
            if (!direcKeys.Contains(directionKey))
                return directionKey;
            var angle = _displayAngle % 360;
            var keyNumber = ((int)directionKey - (int)Keys.Left) % 4;
            switch (angle)
            {
                case 270:
                    keyNumber += 1;
                    break;
                case 180:
                    keyNumber += 2;
                    break;
                case 90:
                    keyNumber += 3;
                    break;
                default: break;
            }
            keyNumber = keyNumber % 4;
            return (Keys)((int)Keys.Left + keyNumber);
        }
 
 
    }
}