프로젝트

일반

사용자정보

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

markus / KCOM / Controls / DecodeImage.cs @ 99b9f32a

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

1 566f0526 taeseongkim
using KCOM.Common;
2
using System;
3 92442e4a taeseongkim
using System.Collections.Generic;
4
5
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8
using System.Windows;
9
using System.Windows.Controls;
10
using System.Windows.Media;
11
using System.Windows.Media.Imaging;
12
13
namespace KCOM.Controls
14
{
15 09b40aad taeseongkim
   // [TemplatePart(Name = PART_IMAGE, Type = typeof(Image))]
16 92442e4a taeseongkim
    public class ScaleDecodeImage : System.Windows.Controls.Control
17
    {
18 81173d69 taeseongkim
        // private const string PART_IMAGE = "PART_IMAGE";
19 09b40aad taeseongkim
        //private Image ImageControl;
20 81173d69 taeseongkim
        private List<ScaleImage> scaleImages = new List<ScaleImage>();
21 92442e4a taeseongkim
22
        public override void OnApplyTemplate()
23
        {
24
            base.OnApplyTemplate();
25
26 09b40aad taeseongkim
            //ImageControl = GetTemplateChild(PART_IMAGE) as Image;
27 92442e4a taeseongkim
        }
28
29
30 09b40aad taeseongkim
        public static readonly DependencyProperty ViewSourceProperty = DependencyProperty.Register(
31 eb9a8cb6 taeseongkim
                    "ViewSource", typeof(ImageSource), typeof(ScaleDecodeImage));
32 09b40aad taeseongkim
33
34 eb9a8cb6 taeseongkim
        public ImageSource ViewSource
35 09b40aad taeseongkim
        {
36 eb9a8cb6 taeseongkim
            get => (ImageSource)GetValue(ViewSourceProperty);
37 09b40aad taeseongkim
            set => SetValue(ViewSourceProperty, value);
38
        }
39
40 92442e4a taeseongkim
        public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(
41 09b40aad taeseongkim
                    "Source", typeof(BitmapImage), typeof(ScaleDecodeImage), new PropertyMetadata(new PropertyChangedCallback(SourcePropertyChaged)));
42 92442e4a taeseongkim
43
44
        public BitmapImage Source
45
        {
46
            get => (BitmapImage)GetValue(SourceProperty);
47
            set => SetValue(SourceProperty, value);
48
        }
49
50
        private static void SourcePropertyChaged(DependencyObject d, DependencyPropertyChangedEventArgs e)
51
        {
52
            var owner = d as ScaleDecodeImage;
53
54 09b40aad taeseongkim
            if (owner != null)
55 92442e4a taeseongkim
            {
56 09b40aad taeseongkim
                if (e.NewValue != null)
57
                {
58 81173d69 taeseongkim
                    owner.scaleImages = new List<ScaleImage>();
59 09b40aad taeseongkim
                    owner.ViewSource = (BitmapImage)e.NewValue;
60
                    owner.Scale = 1.0;
61
                }
62 92442e4a taeseongkim
            }
63
        }
64
65
        public static readonly DependencyProperty ScaleProperty = DependencyProperty.Register(
66 09b40aad taeseongkim
                    "Scale", typeof(double), typeof(ScaleDecodeImage), new PropertyMetadata(0.0, new PropertyChangedCallback(ScalePropertyChaged)));
67 92442e4a taeseongkim
68
69
        public double Scale
70
        {
71
            get => (double)GetValue(ScaleProperty);
72
            set => SetValue(ScaleProperty, value);
73
        }
74
75 566f0526 taeseongkim
        private static async void ScalePropertyChaged(DependencyObject d, DependencyPropertyChangedEventArgs e)
76 92442e4a taeseongkim
        {
77
            var owner = d as ScaleDecodeImage;
78
79
            if (owner != null)
80
            {
81 81173d69 taeseongkim
                var value = owner.Scale;
82
                var scaleImage = owner.scaleImages.Where(x => x.Scale == value);
83 566f0526 taeseongkim
84 81173d69 taeseongkim
                if (scaleImage.Count() > 0)
85
                {
86
                    owner.ViewSource = scaleImage.First().source;
87
                }
88
                else
89
                {
90
                    await owner.Dispatcher.InvokeAsync(() =>
91 566f0526 taeseongkim
                    {
92
93 81173d69 taeseongkim
                        if (value < 1)
94
                        {
95
                            TransformedBitmap tb = new TransformedBitmap();
96
                            tb.BeginInit();
97
                            tb.Source = owner.Source;
98
                            tb.Transform = new ScaleTransform(value, value);
99
                            tb.EndInit();
100 566f0526 taeseongkim
101 997071b8 taeseongkim
                            owner.ViewSource = tb;
102
103
                            #region 진하게 하는 부분은 해제   김태성  2019.07.10
104 35b00484 djkim
105 997071b8 taeseongkim
                            /*
106 81173d69 taeseongkim
                            byte[] pixels = ImageSourceHelper.CopyPixels(tb);
107 566f0526 taeseongkim
108 fc9da8df taeseongkim
                            var brightness = (double)(value * 100) / (double)100 * (double)100 - 10;
109 566f0526 taeseongkim
110 fc9da8df taeseongkim
                            if (brightness < 0)
111 81173d69 taeseongkim
                            {
112 997071b8 taeseongkim
                                var pixels2 = ImageSourceHelper.SetBrightnessAndContrast((int)brightness, brightness * -1.0, pixels);
113
114
                                 pixels = ImageSourceHelper.SetBrightnessAndContrastLinp((int)brightness, brightness * -1.0, pixels);
115
116
                                System.Diagnostics.Debug.WriteLine("pixels 비교 : " + pixels.SequenceEqual(pixels2));
117
118 81173d69 taeseongkim
                                //pixels = ImageSourceHelper.SetContrast(brightness * -1.0, pixels);
119 566f0526 taeseongkim
120 81173d69 taeseongkim
                                var bmp = new WriteableBitmap(tb.PixelWidth, tb.PixelHeight, tb.DpiX, tb.DpiY, PixelFormats.Pbgra32, null);
121
                                bmp.WritePixels(new Int32Rect(0, 0, tb.PixelWidth, tb.PixelHeight), pixels, tb.PixelWidth * 4, 0);
122
                                owner.ViewSource = bmp;
123
                            }
124
                            else
125
                            {
126
                                owner.ViewSource = tb;
127
                            }
128 30ad1266 djkim
129 35b00484 djkim
                            System.Diagnostics.Debug.WriteLine($"TranFormImage Height : {tb.Height} Scale : {value}  brightness : {brightness}");
130 997071b8 taeseongkim
                            */
131
                            #endregion
132 81173d69 taeseongkim
133
                            owner.scaleImages.Add(new ScaleImage { Scale = value, source = owner.ViewSource });
134
135 6396f27e taeseongkim
                            //System.Diagnostics.Debug.WriteLine($"TranFormImage Height : {tb.Height} Scale : {value}  brightness : {brightness}");
136 81173d69 taeseongkim
137
                        }
138
                    });
139
                }
140 92442e4a taeseongkim
            }
141
        }
142
    }
143 81173d69 taeseongkim
144
    public class ScaleImage
145
    {
146
        public ImageSource source { get; set; }
147
        public double Scale { get; set; }
148
    }
149 92442e4a taeseongkim
}
클립보드 이미지 추가 (최대 크기: 500 MB)