프로젝트

일반

사용자정보

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

markus / KCOM / Controls / DecodeImage.cs @ e1c892f7

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

1
using KCOM.Common;
2
using System;
3
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
   // [TemplatePart(Name = PART_IMAGE, Type = typeof(Image))]
16
    public class ScaleDecodeImage : System.Windows.Controls.Control
17
    {
18
        // private const string PART_IMAGE = "PART_IMAGE";
19
        //private Image ImageControl;
20
        private List<ScaleImage> scaleImages = new List<ScaleImage>();
21

    
22
        public override void OnApplyTemplate()
23
        {
24
            base.OnApplyTemplate();
25

    
26
            //ImageControl = GetTemplateChild(PART_IMAGE) as Image;
27
        }
28

    
29

    
30
        public static readonly DependencyProperty ViewSourceProperty = DependencyProperty.Register(
31
                    "ViewSource", typeof(ImageSource), typeof(ScaleDecodeImage));
32

    
33

    
34
        public ImageSource ViewSource
35
        {
36
            get => (ImageSource)GetValue(ViewSourceProperty);
37
            set => SetValue(ViewSourceProperty, value);
38
        }
39

    
40
        public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(
41
                    "Source", typeof(BitmapImage), typeof(ScaleDecodeImage), new PropertyMetadata(new PropertyChangedCallback(SourcePropertyChaged)));
42

    
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
            if (owner != null)
55
            {
56
                if (e.NewValue != null)
57
                {
58
                    owner.scaleImages = new List<ScaleImage>();
59
                    owner.ViewSource = (BitmapImage)e.NewValue;
60
                    owner.Scale = 1.0;
61
                }
62
            }
63
        }
64

    
65
        public static readonly DependencyProperty ScaleProperty = DependencyProperty.Register(
66
                    "Scale", typeof(double), typeof(ScaleDecodeImage), new PropertyMetadata(0.0, new PropertyChangedCallback(ScalePropertyChaged)));
67

    
68

    
69
        public double Scale
70
        {
71
            get => (double)GetValue(ScaleProperty);
72
            set => SetValue(ScaleProperty, value);
73
        }
74

    
75
        private static async void ScalePropertyChaged(DependencyObject d, DependencyPropertyChangedEventArgs e)
76
        {
77
            var owner = d as ScaleDecodeImage;
78

    
79
            if (owner != null)
80
            {
81
                var value = owner.Scale;
82
                var scaleImage = owner.scaleImages.Where(x => x.Scale == value);
83

    
84
                if (scaleImage.Count() > 0)
85
                {
86
                    owner.ViewSource = scaleImage.First().source;
87
                }
88
                else
89
                {
90
                    await owner.Dispatcher.InvokeAsync(() =>
91
                    {
92

    
93
                        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

    
101
                            owner.ViewSource = tb;
102

    
103
                            #region 진하게 하는 부분은 해제   김태성  2019.07.10
104

    
105
                            /*
106
                            byte[] pixels = ImageSourceHelper.CopyPixels(tb);
107

    
108
                            var brightness = (double)(value * 100) / (double)100 * (double)100 - 10;
109

    
110
                            if (brightness < 0)
111
                            {
112
                                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
                                //pixels = ImageSourceHelper.SetContrast(brightness * -1.0, pixels);
119

    
120
                                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

    
129
                            System.Diagnostics.Debug.WriteLine($"TranFormImage Height : {tb.Height} Scale : {value}  brightness : {brightness}");
130
                            */
131
                            #endregion
132

    
133
                            owner.scaleImages.Add(new ScaleImage { Scale = value, source = owner.ViewSource });
134

    
135
                            //System.Diagnostics.Debug.WriteLine($"TranFormImage Height : {tb.Height} Scale : {value}  brightness : {brightness}");
136

    
137
                        }
138
                    });
139
                }
140
            }
141
        }
142
    }
143

    
144
    public class ScaleImage
145
    {
146
        public ImageSource source { get; set; }
147
        public double Scale { get; set; }
148
    }
149
}
클립보드 이미지 추가 (최대 크기: 500 MB)