프로젝트

일반

사용자정보

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

markus / KCOM / Controls / DecodeImage.cs @ 566f0526

이력 | 보기 | 이력해설 | 다운로드 (4.01 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

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

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

    
28

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

    
32

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

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

    
42

    
43
        public BitmapImage Source
44
        {
45
            get => (BitmapImage)GetValue(SourceProperty);
46
            set => SetValue(SourceProperty, value);
47
        }
48

    
49
        private static void SourcePropertyChaged(DependencyObject d, DependencyPropertyChangedEventArgs e)
50
        {
51
            var owner = d as ScaleDecodeImage;
52

    
53
            if (owner != null)
54
            {
55
                if (e.NewValue != null)
56
                {
57
                    owner.ViewSource = (BitmapImage)e.NewValue;
58
                    owner.Scale = 1.0;
59
                }
60
            }
61
        }
62

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

    
66

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

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

    
77
            if (owner != null)
78
            {
79
                await owner.Dispatcher.InvokeAsync(() => {
80

    
81
                    var value = owner.Scale;
82

    
83
                    if (value < 1)
84
                    {
85
                        TransformedBitmap tb = new TransformedBitmap();
86
                        tb.BeginInit();
87
                        tb.Source = owner.Source;
88
                        tb.Transform = new ScaleTransform(value, value);
89
                        tb.EndInit();
90

    
91
                        byte[] pixels = ImageSourceHelper.CopyPixels(tb);
92

    
93
                        var brightness = (double)(value * 100) / (double)100 * (double)100 - 80;
94

    
95
                        if (brightness < -30)
96
                        {
97
                            pixels = ImageSourceHelper.SetBrightnessAndContrast((int)brightness, brightness * -1.0, pixels);
98
                            //pixels = ImageSourceHelper.SetContrast(brightness * -1.0, pixels);
99

    
100
                            var bmp = new WriteableBitmap(tb.PixelWidth, tb.PixelHeight, tb.DpiX, tb.DpiY, PixelFormats.Pbgra32, null);
101
                            bmp.WritePixels(new Int32Rect(0, 0, tb.PixelWidth, tb.PixelHeight), pixels, tb.PixelWidth * 4, 0);
102
                            owner.ViewSource = bmp;
103
                        }
104
                        else
105
                        {
106
                            owner.ViewSource = tb;
107
                        }
108

    
109
                        System.Diagnostics.Debug.WriteLine($"TranFormImage Height : {tb.Height} Scale : {value}  brightness : {brightness}");
110
                    
111
                    }
112
                });
113
            }
114
        }
115
    }
116
}
클립보드 이미지 추가 (최대 크기: 500 MB)