프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkupToPDF / Controls / Etc / ImgControl.cs @ 554aae3b

이력 | 보기 | 이력해설 | 다운로드 (19.5 KB)

1
using System;
2
using System.Net;
3
using System.Windows;
4
using System.Windows.Controls;
5
using System.Windows.Documents;
6
using System.Windows.Ink;
7
using System.Windows.Input;
8
using System.Windows.Media;
9
using System.Windows.Media.Animation;
10
using System.Windows.Shapes;
11
using System.Collections.Generic;
12
using System.ComponentModel;
13
using System.Linq;
14
using MarkupToPDF.Controls.Common;
15
using MarkupToPDF.Common;
16
using MarkupToPDF.Serialize.Core;
17
using MarkupToPDF.Serialize.S_Control;
18
using System.Windows.Media.Imaging;
19
using Svg2Xaml;
20

    
21
namespace MarkupToPDF.Controls.Etc
22
{
23
    public class ImgControl : CommentUserInfo, IDisposable, INormal, INotifyPropertyChanged, IViewBox, IMarkupCommonData
24
    {
25
        #region 초기선언
26
        private const string PART_Image = "PART_Image";
27
        public Image Base_Image = null;
28
        #endregion
29

    
30
        static ImgControl()
31
        {
32
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImgControl), new FrameworkPropertyMetadata(typeof(ImgControl)));
33
            //Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/MarkupToPDF;Component/Themes/generic.xaml")) as ResourceDictionary);
34
            ResourceDictionary dictionary = new ResourceDictionary();
35
            dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute);
36
            Application.Current.Resources.MergedDictionaries.Add(dictionary);
37
             //System.Diagnostics.Debug.WriteLine("resource Count :" + Application.Current.Resources.MergedDictionaries.Count);
38
        }
39

    
40
        public ImgControl()
41
        {
42
            this.DefaultStyleKey = typeof(ImgControl);
43
        }
44

    
45
        public void Dispose()
46
        {
47
            GC.Collect();
48
            GC.SuppressFinalize(this);
49
        }
50
        public event PropertyChangedEventHandler PropertyChanged;
51
        protected void OnPropertyChanged(string propName)
52
        {
53
            if (PropertyChanged != null)
54
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
55
        }
56

    
57
        public override void ApplyOverViewData()
58
        {
59
            this.OverViewPathData = this.PathData;
60
        }
61

    
62
        #region Dependency Properties
63

    
64
        public static readonly DependencyProperty IsSelectedProperty =
65
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(ImgControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
66

    
67
        public static readonly DependencyProperty ControlTypeProperty =
68
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(ImgControl), new FrameworkPropertyMetadata(ControlType.ImgControl));
69

    
70
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
71
       "PathData", typeof(Geometry), typeof(ImgControl), null);
72
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
73
        "OverViewPathData", typeof(Geometry), typeof(ImgControl), null);
74

    
75
        public static readonly DependencyProperty ImageDataProperty = DependencyProperty.Register(
76
            "ImageData", typeof(ImageSource), typeof(ImgControl), new PropertyMetadata(null));
77
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
78
            "UserID", typeof(string), typeof(ImgControl), new PropertyMetadata(null));
79
        public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(
80
            "FilePath", typeof(string), typeof(ImgControl), new PropertyMetadata(null));
81
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
82
            "StartPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
83
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
84
            "EndPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(100, 100), PointValueChanged));
85
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
86
            "PointSet", typeof(List<Point>), typeof(ImgControl), new PropertyMetadata(new List<Point>()));
87
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
88
             "TopRightPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
89
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
90
              "LeftBottomPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
91
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(ImgControl),
92
                        new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
93
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(ImgControl),
94
                        new PropertyMetadata((double)0, OnCenterXYChanged));
95
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(ImgControl),
96
                        new PropertyMetadata((double)0, OnCenterXYChanged));
97

    
98
        #endregion
99
        #region PropertyChanged Method
100

    
101
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
102
        {
103
            //var instance = (ImgControl)sender;
104

    
105
            //if (e.OldValue != e.NewValue && instance.Base_Image != null)
106
            //{
107

    
108
            //    instance.SetValue(e.Property, e.NewValue);
109

    
110
            //    if (instance.IsSelected)
111
            //    {
112
            //        //instance.Base_Image.Stroke = new SolidColorBrush(Colors.Blue);
113
            //    }
114
            //    else
115
            //    {
116
            //        //instance.Base_Image.Stroke = new SolidColorBrush(Colors.Red);
117
            //    }
118
            //}
119
        }
120

    
121
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
122
        {
123
            var instance = (ImgControl)sender;
124
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
125
            {
126
                instance.SetValue(e.Property, e.NewValue);
127
                instance.SetImage();
128
            }
129
        }
130
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
131
        {
132
            var instance = (ImgControl)sender;
133
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
134
            {
135
                instance.SetValue(e.Property, e.NewValue);
136
                instance.SetImage();
137
            }
138
        }
139
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
140
        {
141
            var instance = (ImgControl)sender;
142
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
143
            {
144
                instance.SetValue(e.Property, e.NewValue);
145
                instance.SetImage();
146
            }
147
        }
148
        #endregion
149
        #region Properties
150
        public double CenterX
151
        {
152
            get { return (double)GetValue(CenterXProperty); }
153
            set { SetValue(CenterXProperty, value); }
154
        }
155
        public string UserID
156
        {
157
            get { return (string)GetValue(UserIDProperty); }
158
            set
159
            {
160
                if (this.UserID != value)
161
                {
162
                    SetValue(UserIDProperty, value);
163
                    OnPropertyChanged("UserID");
164
                }
165
            }
166
        }
167
       
168
        public double CenterY
169
        {
170
            get { return (double)GetValue(CenterYProperty); }
171
            set { SetValue(CenterYProperty, value); }
172
        }
173
        public string FilePath
174
        {
175
            get { return (string)GetValue(FilePathProperty); }
176
            set { SetValue(FilePathProperty, value); }
177
        }
178
        public Point EndPoint
179
        {
180
            get { return (Point)GetValue(EndPointProperty); }
181
            set
182
            {
183
                if (this.EndPoint != value)
184
                {
185
                    SetValue(EndPointProperty, value);
186
                    OnPropertyChanged("EndPoint");
187
                }
188
            }
189
        }
190
        public List<Point> PointSet
191
        {
192
            get { return (List<Point>)GetValue(PointSetProperty); }
193
            set { SetValue(PointSetProperty, value); }
194
        }
195
        public Point StartPoint
196
        {
197
            get { return (Point)GetValue(StartPointProperty); }
198
            set
199
            {
200
                if (this.StartPoint != value)
201
                {
202
                    SetValue(StartPointProperty, value);
203
                    OnPropertyChanged("StartPoint");
204
                }
205
            }
206
        }
207
        public Point TopRightPoint
208
        {
209
            get { return (Point)GetValue(TopRightPointProperty); }
210
            set
211
            {
212
                SetValue(TopRightPointProperty, value);
213
                OnPropertyChanged("TopRightPoint");
214
            }
215
        }
216
        public Point LeftBottomPoint
217
        {
218
            get { return (Point)GetValue(LeftBottomPointProperty); }
219
            set
220
            {
221
                SetValue(LeftBottomPointProperty, value);
222
                OnPropertyChanged("LeftBottomPoint");
223
            }
224
        }
225

    
226
        public override double Angle
227
        {
228
            get { return (double)GetValue(AngleProperty); }
229
            set
230
            {
231
                if (this.Angle != value)
232
                {
233
                    SetValue(AngleProperty, value);
234
                }
235
            }
236
        }
237
        public ImageSource ImageData
238
        {
239
            get { return (ImageSource)this.GetValue(ImageDataProperty); }
240
            set { this.SetValue(ImageDataProperty, value); }
241
        }
242

    
243
         public override bool IsSelected
244
        {
245
            get
246
            {
247
                return (bool)GetValue(IsSelectedProperty);
248
            }
249
            set
250
            {
251
                SetValue(IsSelectedProperty, value);
252
                //OnPropertyChanged("IsSelected");
253
            }
254
        }
255

    
256
        public override ControlType ControlType
257
        {
258
            set
259
            {
260
                SetValue(ControlTypeProperty, value);
261
                OnPropertyChanged("ControlType");
262
            }
263
            get
264
            {
265
                return (ControlType)GetValue(ControlTypeProperty);
266
            }
267
        }
268

    
269
        public Geometry OverViewPathData
270
        {
271
            get { return (Geometry)GetValue(OverViewPathDataProperty); }
272
            set
273
            {
274
                SetValue(OverViewPathDataProperty, value);
275
                OnPropertyChanged("OverViewPathData");
276
            }
277
        }
278

    
279

    
280
        #endregion
281

    
282
        public override void OnApplyTemplate()
283
        {
284
            base.OnApplyTemplate();
285
            Base_Image = GetTemplateChild(PART_Image) as Image;
286
            Base_Image.Width = 0;
287
            Base_Image.Height = 0;
288
            SetImage();
289
        }
290

    
291
        public void SetImage()
292
        {
293
            this.ApplyTemplate();
294
            Point mid = MathSet.FindCentroid(new List<Point>()
295
            {
296
                this.StartPoint,
297
                this.LeftBottomPoint,
298
                this.EndPoint,
299
                this.TopRightPoint,                
300
            });
301
            double AngleData = this.Angle * -1;
302
            PathFigure pathFigure = new PathFigure();
303

    
304
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
305

    
306
            LineSegment lineSegment0 = new LineSegment();
307
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
308
            pathFigure.Segments.Add(lineSegment0);
309

    
310
            LineSegment lineSegment1 = new LineSegment();
311
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
312
            pathFigure.Segments.Add(lineSegment1);
313

    
314
            LineSegment lineSegment2 = new LineSegment();
315
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
316
            pathFigure.Segments.Add(lineSegment2);
317

    
318
            LineSegment lineSegment3 = new LineSegment();
319
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
320
            pathFigure.Segments.Add(lineSegment3);
321

    
322
            PathGeometry pathGeometry = new PathGeometry();
323
            pathGeometry.Figures = new PathFigureCollection();
324
            pathFigure.IsClosed = true;
325
            pathGeometry.Figures.Add(pathFigure);
326
            this.Base_Image.Width = pathGeometry.Bounds.Width;
327
            this.Base_Image.Height = pathGeometry.Bounds.Height;
328
             this.Tag = pathGeometry;
329

    
330
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_Image.Width / 2);
331
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_Image.Height / 2);
332

    
333
            this.PathData = pathGeometry;
334
            ApplyOverViewData();
335

    
336
        }
337
        public override void UpdateControl()
338
        {
339
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
340
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
341
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
342
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
343
        }
344

    
345
        public double LineSize { get; set; }
346

    
347
        public Geometry PathData { get; set; }
348

    
349
        /// <summary>
350
        /// call when mouse is moving while drawing control
351
        /// </summary>
352
        /// <param name="pt"></param>
353
        /// <param name="bAxisLocked"></param>
354
        public override void OnCreatingMouseMove(Point pt, bool bAxisLocked, bool bShiftKeyPressed)
355
        {
356
            this.EndPoint = pt;
357
            this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
358
            this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
359
            
360
            this.PointSet = new List<Point>
361
            {
362
                this.StartPoint,
363
                this.LeftBottomPoint,
364
                this.EndPoint,
365
                this.TopRightPoint,
366
            };
367
        }
368

    
369
        /// <summary>
370
        /// move control point has same location of given pt along given delta
371
        /// </summary>
372
        /// <author>humkyung</author>
373
        /// <date>2019.06.20</date>
374
        /// <param name="pt"></param>
375
        /// <param name="dx"></param>
376
        /// <param name="dy"></param>
377
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy)
378
        {
379
            IPath path = (this as IPath);
380

    
381
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
382
            selected.X += dx;
383
            selected.Y += dy;
384
            int i = 0;
385
            for (i = 0; i < (this as IPath).PointSet.Count; i++)
386
            {
387
                if (pt.Equals((this as IPath).PointSet[i]))
388
                {
389
                    path.PointSet[i] = selected;
390
                    break;
391
                }
392
            }
393

    
394
            var ReverseP = (i + path.PointSet.Count() / 2) % path.PointSet.Count();
395
            var PreviousP = (i + (path.PointSet.Count() - 1)) % path.PointSet.Count();
396
            var NextP = (i + 1) % path.PointSet.Count();
397

    
398
            var distance = MathSet.DistanceTo(path.PointSet[ReverseP], path.PointSet[i]);
399

    
400
            var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[PreviousP]);
401
            var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X,
402
                path.PointSet[i].Y - path.PointSet[ReverseP].Y);
403
            path.PointSet[PreviousP] = new Point(path.PointSet[ReverseP].X + PreviousV.X * l, path.PointSet[ReverseP].Y + PreviousV.Y * l);
404

    
405
            var NextV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[NextP]);
406
            l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X, path.PointSet
407
                [i].Y - path.PointSet[ReverseP].Y);
408
            path.PointSet[NextP] = new Point(path.PointSet[ReverseP].X + NextV.X * l, path.PointSet[ReverseP].Y + NextV.Y * l);
409

    
410
            this.UpdateControl();
411
        }
412

    
413
        /// <summary>
414
        /// return ImgControl's area
415
        /// </summary>
416
        /// <author>humkyung</author>
417
        /// <date>2019.06.13</date>
418
        public override Rect ItemRect
419
        {
420
            get
421
            {
422
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
423
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
424
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
425
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
426

    
427
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
428
            }
429
        }
430

    
431
        /// <summary>
432
        /// Serialize this
433
        /// </summary>
434
        /// <param name="sUserId"></param>
435
        /// <returns></returns>
436
        public override string Serialize()
437
        {
438
            using (S_ImgControl STemp = new S_ImgControl())
439
            {
440
                STemp.Angle = this.Angle;
441
                STemp.EndPoint = this.EndPoint;
442
                STemp.LB = this.LeftBottomPoint;
443
                STemp.Name = this.GetType().Name;
444
                STemp.PointSet = this.PointSet;
445
                STemp.StartPoint = this.StartPoint;
446
                STemp.UserID = this.UserID;
447
                STemp.Opac = this.Opacity;
448
                STemp.TR = this.TopRightPoint;
449
                STemp.ImagePath = this.FilePath;
450
                ///강인구 추가(2017.11.02)
451
                ///Memo 추가
452
                STemp.Memo = this.Memo;
453

    
454
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
455
            }
456
        }
457

    
458
        /// <summary>
459
        /// create a imgcontrol from given string
460
        /// </summary>
461
        /// <param name="str"></param>
462
        /// <returns></returns>
463
        public static ImgControl FromString(string str, SolidColorBrush brush, string sProjectNo)
464
        {
465
            ImgControl instance = null;
466
            using (S_ImgControl s = JsonSerializerHelper.JsonDeserialize<S_ImgControl>(str))
467
            {
468
                Image img = new Image();
469
                if (s.ImagePath.Contains(".svg"))
470
                {
471
                    byte[] imageData = null;
472
                    DrawingImage image = null;
473
                    using (System.Net.WebClient web = new System.Net.WebClient())
474
                    {
475
                        imageData = web.DownloadData(new Uri(s.ImagePath));
476
                        System.IO.Stream stream = new System.IO.MemoryStream(imageData);
477
                        image = SvgReader.Load(stream);
478
                    }
479
                    img.Source = image;
480
                }
481
                else
482
                {
483
                    img.Source = new BitmapImage(new Uri(s.ImagePath));
484
                }
485

    
486
                instance = new ImgControl
487
                {
488
                    Angle = s.Angle,
489
                    StartPoint = s.StartPoint,
490
                    TopRightPoint = s.TR,
491
                    EndPoint = s.EndPoint,
492
                    LeftBottomPoint = s.LB,
493
                    PointSet = s.PointSet,
494
                    Opacity = s.Opac,
495
                    FilePath = s.ImagePath,
496
                    UserID = s.UserID,
497
                    ImageData = img.Source,
498
                    Memo = s.Memo 
499
                };
500

    
501
                instance.ImageData = img.Source;
502
            }
503

    
504
            return instance;
505
        }
506
    }
507
}
클립보드 이미지 추가 (최대 크기: 500 MB)