프로젝트

일반

사용자정보

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

markus / KCOM / Controls / DecodeImage.cs @ 81173d69

이력 | 보기 | 이력해설 | 다운로드 (4.75 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
                            byte[] pixels = ImageSourceHelper.CopyPixels(tb);
102

    
103
                            var brightness = (double)(value * 100) / (double)100 * (double)100 - 80;
104

    
105
                            if (brightness < -30)
106
                            {
107
                                pixels = ImageSourceHelper.SetBrightnessAndContrast((int)brightness, brightness * -1.0, pixels);
108
                                //pixels = ImageSourceHelper.SetContrast(brightness * -1.0, pixels);
109

    
110
                                var bmp = new WriteableBitmap(tb.PixelWidth, tb.PixelHeight, tb.DpiX, tb.DpiY, PixelFormats.Pbgra32, null);
111
                                bmp.WritePixels(new Int32Rect(0, 0, tb.PixelWidth, tb.PixelHeight), pixels, tb.PixelWidth * 4, 0);
112
                                owner.ViewSource = bmp;
113
                            }
114
                            else
115
                            {
116
                                owner.ViewSource = tb;
117
                            }
118

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

    
121
                            System.Diagnostics.Debug.WriteLine($"TranFormImage Height : {tb.Height} Scale : {value}  brightness : {brightness}");
122

    
123
                        }
124
                    });
125
                }
126
            }
127
        }
128
    }
129

    
130
    public class ScaleImage
131
    {
132
        public ImageSource source { get; set; }
133
        public double Scale { get; set; }
134
    }
135
}
클립보드 이미지 추가 (최대 크기: 500 MB)