프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Etc / ImgControl.cs @ 661b7416

이력 | 보기 | 이력해설 | 다운로드 (16.1 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 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
        public double Angle
226
        {
227
            get { return (double)GetValue(AngleProperty); }
228
            set
229
            {
230
                if (this.Angle != value)
231
                {
232
                    SetValue(AngleProperty, value);
233
                }
234
            }
235
        }
236
        public ImageSource ImageData
237
        {
238
            get { return (ImageSource)this.GetValue(ImageDataProperty); }
239
            set { this.SetValue(ImageDataProperty, value); }
240
        }
241

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

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

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

    
278

    
279
        #endregion
280

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

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

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

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

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

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

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

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

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

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

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

    
344
        public double LineSize { get; set; }
345

    
346
        public Geometry PathData { get; set; }
347

    
348
        /// <summary>
349
        /// Serialize this
350
        /// </summary>
351
        /// <param name="sUserId"></param>
352
        /// <returns></returns>
353
        public override string Serialize()
354
        {
355
            using (S_ImgControl STemp = new S_ImgControl())
356
            {
357
                STemp.Angle = this.Angle;
358
                STemp.EndPoint = this.EndPoint;
359
                STemp.LB = this.LeftBottomPoint;
360
                STemp.Name = this.GetType().Name;
361
                STemp.PointSet = this.PointSet;
362
                STemp.StartPoint = this.StartPoint;
363
                STemp.UserID = this.UserID;
364
                STemp.Opac = this.Opacity;
365
                STemp.TR = this.TopRightPoint;
366
                STemp.ImagePath = this.FilePath;
367
                ///강인구 추가(2017.11.02)
368
                ///Memo 추가
369
                STemp.Memo = this.Memo;
370

    
371
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
372
            }
373
        }
374

    
375
        /// <summary>
376
        /// create a imgcontrol from given string
377
        /// </summary>
378
        /// <param name="str"></param>
379
        /// <returns></returns>
380
        public static ImgControl FromString(string str, SolidColorBrush brush, string sProjectNo)
381
        {
382
            ImgControl instance = null;
383
            using (S_ImgControl s = JsonSerializerHelper.JsonDeserialize<S_ImgControl>(str))
384
            {
385
                Image img = new Image();
386
                if (s.ImagePath.Contains(".svg"))
387
                {
388
                    byte[] imageData = null;
389
                    DrawingImage image = null;
390
                    using (System.Net.WebClient web = new System.Net.WebClient())
391
                    {
392
                        imageData = web.DownloadData(new Uri(s.ImagePath));
393
                        System.IO.Stream stream = new System.IO.MemoryStream(imageData);
394
                        image = SvgReader.Load(stream);
395
                    }
396
                    img.Source = image;
397
                }
398
                else
399
                {
400
                    img.Source = new BitmapImage(new Uri(s.ImagePath));
401
                }
402

    
403
                instance = new ImgControl
404
                {
405
                    Angle = s.Angle,
406
                    StartPoint = s.StartPoint,
407
                    TopRightPoint = s.TR,
408
                    EndPoint = s.EndPoint,
409
                    LeftBottomPoint = s.LB,
410
                    PointSet = s.PointSet,
411
                    Opacity = s.Opac,
412
                    FilePath = s.ImagePath,
413
                    UserID = s.UserID,
414
                    ImageData = img.Source,
415
                    Memo = s.Memo 
416
                };
417

    
418
                instance.ImageData = img.Source;
419
            }
420

    
421
            return instance;
422
        }
423
    }
424
}
클립보드 이미지 추가 (최대 크기: 500 MB)