markus / KCOM / Common / Converter / SvgImageConverter.cs @ fd452a01
이력 | 보기 | 이력해설 | 다운로드 (7.92 KB)
1 | f1f822e9 | taeseongkim | using SharpVectors.Converters; |
---|---|---|---|
2 | using SharpVectors.Dom.Utils; |
||
3 | a74e3cbc | ljiyeon | using System; |
4 | using System.Collections.Generic; |
||
5 | f1f822e9 | taeseongkim | using System.ComponentModel; |
6 | a74e3cbc | ljiyeon | using System.Globalization; |
7 | f1f822e9 | taeseongkim | using System.IO; |
8 | a74e3cbc | ljiyeon | using System.Linq; |
9 | using System.Text; |
||
10 | using System.Threading.Tasks; |
||
11 | using System.Windows.Controls; |
||
12 | using System.Windows.Data; |
||
13 | f1f822e9 | taeseongkim | using System.Windows.Markup; |
14 | a74e3cbc | ljiyeon | using System.Windows.Media; |
15 | using System.Windows.Media.Imaging; |
||
16 | |||
17 | namespace KCOM.Common.Converter |
||
18 | { |
||
19 | f1f822e9 | taeseongkim | public class SvgImageConverter : SvgImageBase, IValueConverter |
20 | a74e3cbc | ljiyeon | { |
21 | f1f822e9 | taeseongkim | #region Private Fields |
22 | |||
23 | private Uri _baseUri; |
||
24 | private readonly UriTypeConverter _uriConverter; |
||
25 | |||
26 | #endregion |
||
27 | |||
28 | public SvgImageConverter() |
||
29 | { |
||
30 | _uriConverter = new UriTypeConverter(); |
||
31 | } |
||
32 | f65e6c02 | taeseongkim | |
33 | public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
||
34 | a74e3cbc | ljiyeon | { |
35 | 3b938959 | taeseongkim | ImageSource imageSource = null; |
36 | |||
37 | f1f822e9 | taeseongkim | try |
38 | f65e6c02 | taeseongkim | { |
39 | f1f822e9 | taeseongkim | if (string.IsNullOrWhiteSpace(_appName)) |
40 | { |
||
41 | if (this.IsDesignMode() || LicenseManager.UsageMode == LicenseUsageMode.Designtime) |
||
42 | { |
||
43 | this.GetAppName(); |
||
44 | } |
||
45 | } |
||
46 | f65e6c02 | taeseongkim | |
47 | f1f822e9 | taeseongkim | Uri inputUri = null; |
48 | if (parameter != null) |
||
49 | { |
||
50 | inputUri = this.ResolveUri(parameter.ToString()); |
||
51 | } |
||
52 | else if (value != null) |
||
53 | { |
||
54 | inputUri = _uriConverter.ConvertFrom(value) as Uri; |
||
55 | if (inputUri == null) |
||
56 | { |
||
57 | inputUri = this.ResolveUri(value.ToString()); |
||
58 | } |
||
59 | else if (!inputUri.IsAbsoluteUri) |
||
60 | { |
||
61 | inputUri = this.ResolveUri(value.ToString()); |
||
62 | } |
||
63 | } |
||
64 | a74e3cbc | ljiyeon | |
65 | f1f822e9 | taeseongkim | if (inputUri == null) |
66 | a74e3cbc | ljiyeon | { |
67 | f1f822e9 | taeseongkim | return null; |
68 | } |
||
69 | var svgSource = inputUri.IsAbsoluteUri ? inputUri : new Uri(_baseUri, inputUri); |
||
70 | |||
71 | if (svgSource.ToString().EndsWith(".svg")) |
||
72 | { |
||
73 | 3b938959 | taeseongkim | //SharpVectors.Converters.SvgImageExtension svgImage = new SharpVectors.Converters.SvgImageExtension(svgSource.ToString()); |
74 | |||
75 | //return (DrawingImage)svgImage.ProvideValue(null); |
||
76 | //var context = TaskScheduler.FromCurrentSynchronizationContext(); |
||
77 | |||
78 | //var task = Task.Factory.StartNew<SharpVectors.Converters.SvgImageExtension>(() => |
||
79 | //{ |
||
80 | // return new SharpVectors.Converters.SvgImageExtension(svgSource.ToString()); |
||
81 | |||
82 | //},TaskCreationOptions.RunContinuationsAsynchronously).ContinueWith((x)=> |
||
83 | //{ |
||
84 | // return (DrawingImage)x.Result.ProvideValue(null); |
||
85 | //},context); |
||
86 | |||
87 | //imageSource = task.Result; |
||
88 | |||
89 | imageSource = this.GetImage(svgSource); |
||
90 | //return this.GetImage(svgSource); |
||
91 | f1f822e9 | taeseongkim | } |
92 | else |
||
93 | { |
||
94 | 3b938959 | taeseongkim | imageSource = new BitmapImage(new Uri(value.ToString())); |
95 | a74e3cbc | ljiyeon | } |
96 | |||
97 | } |
||
98 | f1f822e9 | taeseongkim | catch |
99 | a74e3cbc | ljiyeon | { |
100 | f1f822e9 | taeseongkim | //throw; #82 |
101 | a74e3cbc | ljiyeon | } |
102 | 3b938959 | taeseongkim | |
103 | return imageSource; |
||
104 | a74e3cbc | ljiyeon | } |
105 | f65e6c02 | taeseongkim | |
106 | public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
||
107 | a74e3cbc | ljiyeon | { |
108 | throw new NotImplementedException(); |
||
109 | } |
||
110 | f1f822e9 | taeseongkim | |
111 | public override object ProvideValue(IServiceProvider serviceProvider) |
||
112 | { |
||
113 | var uriContext = serviceProvider.GetService(typeof(IUriContext)) as IUriContext; |
||
114 | if (uriContext != null) |
||
115 | { |
||
116 | _baseUri = uriContext.BaseUri; |
||
117 | } |
||
118 | |||
119 | return this; |
||
120 | } |
||
121 | |||
122 | |||
123 | #region Private Methods |
||
124 | |||
125 | /// <summary> |
||
126 | /// Converts the SVG source file to <see cref="Uri"/> |
||
127 | /// </summary> |
||
128 | /// <param name="inputParameter"> |
||
129 | /// Object that can provide services for the markup extension. |
||
130 | /// </param> |
||
131 | /// <returns> |
||
132 | /// Returns the valid <see cref="Uri"/> of the SVG source path if |
||
133 | /// successful; otherwise, it returns <see langword="null"/>. |
||
134 | /// </returns> |
||
135 | private Uri ResolveUri(string inputParameter) |
||
136 | { |
||
137 | if (string.IsNullOrWhiteSpace(inputParameter)) |
||
138 | { |
||
139 | return null; |
||
140 | } |
||
141 | if (string.IsNullOrWhiteSpace(_appName)) |
||
142 | { |
||
143 | this.GetAppName(); |
||
144 | } |
||
145 | var comparer = StringComparison.OrdinalIgnoreCase; |
||
146 | |||
147 | Uri svgSource; |
||
148 | if (Uri.TryCreate(inputParameter, UriKind.RelativeOrAbsolute, out svgSource)) |
||
149 | { |
||
150 | if (svgSource.IsAbsoluteUri) |
||
151 | { |
||
152 | return svgSource; |
||
153 | } |
||
154 | // Try getting a local file in the same directory.... |
||
155 | string svgPath = inputParameter; |
||
156 | if (inputParameter[0] == '\\' || inputParameter[0] == '/') |
||
157 | { |
||
158 | svgPath = inputParameter.Substring(1); |
||
159 | } |
||
160 | svgPath = svgPath.Replace('/', '\\'); |
||
161 | |||
162 | var assembly = this.GetExecutingAssembly(); |
||
163 | if (assembly != null) |
||
164 | { |
||
165 | string localFile = PathUtils.Combine(assembly, svgPath); |
||
166 | |||
167 | if (File.Exists(localFile)) |
||
168 | { |
||
169 | return new Uri(localFile); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | // Try getting it as resource file... |
||
174 | var inputUri = _uriConverter.ConvertFrom(inputParameter) as Uri; |
||
175 | if (inputUri != null) |
||
176 | { |
||
177 | if (inputUri.IsAbsoluteUri) |
||
178 | { |
||
179 | return inputUri; |
||
180 | } |
||
181 | if (_baseUri != null) |
||
182 | { |
||
183 | var validUri = new Uri(_baseUri, inputUri); |
||
184 | if (validUri.IsAbsoluteUri) |
||
185 | { |
||
186 | if (validUri.IsFile && File.Exists(validUri.LocalPath)) |
||
187 | { |
||
188 | return validUri; |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | |||
194 | string asmName = _appName; |
||
195 | if (string.IsNullOrWhiteSpace(asmName) && assembly != null) |
||
196 | { |
||
197 | // It should not be the SharpVectors.Converters.Wpf.dll, which contains this extension... |
||
198 | string tempName = assembly.GetName().Name; |
||
199 | if (!string.Equals("SharpVectors.Converters.Wpf", tempName, comparer) |
||
200 | && !string.Equals("WpfSurface", tempName, comparer)) |
||
201 | { |
||
202 | asmName = tempName; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | svgPath = inputParameter; |
||
207 | if (inputParameter.StartsWith("/", comparer)) |
||
208 | { |
||
209 | svgPath = svgPath.TrimStart('/'); |
||
210 | } |
||
211 | |||
212 | // A little hack to display preview in design mode |
||
213 | if (this.IsDesignMode() && !string.IsNullOrWhiteSpace(_appName)) |
||
214 | { |
||
215 | //string uriDesign = string.Format("/{0};component/{1}", _appName, svgPath); |
||
216 | //return new Uri(uriDesign, UriKind.Relative); |
||
217 | |||
218 | // The relative path is not working with the Converter... |
||
219 | string uriDesign = string.Format("pack://application:,,,/{0};component/{1}", |
||
220 | _appName, svgPath); |
||
221 | |||
222 | return new Uri(uriDesign); |
||
223 | } |
||
224 | |||
225 | string uriString = string.Format("pack://application:,,,/{0};component/{1}", |
||
226 | asmName, svgPath); |
||
227 | |||
228 | return new Uri(uriString); |
||
229 | } |
||
230 | |||
231 | return null; |
||
232 | } |
||
233 | |||
234 | #endregion |
||
235 | a74e3cbc | ljiyeon | } |
236 | } |