markus / KCOM / Controls / DecodeImage.cs @ 92442e4a
이력 | 보기 | 이력해설 | 다운로드 (2.43 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
|
4 |
using System.Linq; |
5 |
using System.Text; |
6 |
using System.Threading.Tasks; |
7 |
using System.Windows; |
8 |
using System.Windows.Controls; |
9 |
using System.Windows.Media; |
10 |
using System.Windows.Media.Imaging; |
11 |
|
12 |
namespace KCOM.Controls |
13 |
{ |
14 |
[TemplatePart(Name = PART_IMAGE, Type = typeof(Image))] |
15 |
public class ScaleDecodeImage : System.Windows.Controls.Control |
16 |
{ |
17 |
private const string PART_IMAGE = "PART_IMAGE"; |
18 |
|
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 SourceProperty = DependencyProperty.Register( |
30 |
"Source", typeof(BitmapImage), typeof(ScaleDecodeImage),new PropertyMetadata(new PropertyChangedCallback(SourcePropertyChaged))); |
31 |
|
32 |
|
33 |
public BitmapImage Source |
34 |
{ |
35 |
get => (BitmapImage)GetValue(SourceProperty); |
36 |
set => SetValue(SourceProperty, value); |
37 |
} |
38 |
|
39 |
private static void SourcePropertyChaged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
40 |
{ |
41 |
var owner = d as ScaleDecodeImage; |
42 |
|
43 |
if(owner != null) |
44 |
{ |
45 |
owner.Scale = 1.0; |
46 |
} |
47 |
} |
48 |
|
49 |
public static readonly DependencyProperty ScaleProperty = DependencyProperty.Register( |
50 |
"Scale", typeof(double), typeof(ScaleDecodeImage), new PropertyMetadata(0.0,new PropertyChangedCallback(ScalePropertyChaged))); |
51 |
|
52 |
|
53 |
public double Scale |
54 |
{ |
55 |
get => (double)GetValue(ScaleProperty); |
56 |
set => SetValue(ScaleProperty, value); |
57 |
} |
58 |
|
59 |
private static async void ScalePropertyChaged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
60 |
{ |
61 |
var owner = d as ScaleDecodeImage; |
62 |
|
63 |
if (owner != null) |
64 |
{ |
65 |
if(owner.ImageControl != null) |
66 |
{ |
67 |
var value = owner.Scale; |
68 |
System.Windows.Media.Imaging.BitmapImage image = owner.Source.Clone(); |
69 |
|
70 |
image.DecodePixelWidth = (int)(image.PixelWidth * value); |
71 |
image.DecodePixelHeight = (int)(image.PixelHeight * value); |
72 |
|
73 |
await owner.Dispatcher.InvokeAsync(() => { owner.ImageControl.Source = image; }); |
74 |
|
75 |
} |
76 |
} |
77 |
} |
78 |
} |
79 |
} |