markus / KCOM / Common / Converter / SvgImageConverter.cs @ 3b938959
이력 | 보기 | 이력해설 | 다운로드 (7.92 KB)
1 |
using SharpVectors.Converters; |
---|---|
2 |
using SharpVectors.Dom.Utils; |
3 |
using System; |
4 |
using System.Collections.Generic; |
5 |
using System.ComponentModel; |
6 |
using System.Globalization; |
7 |
using System.IO; |
8 |
using System.Linq; |
9 |
using System.Text; |
10 |
using System.Threading.Tasks; |
11 |
using System.Windows.Controls; |
12 |
using System.Windows.Data; |
13 |
using System.Windows.Markup; |
14 |
using System.Windows.Media; |
15 |
using System.Windows.Media.Imaging; |
16 |
|
17 |
namespace KCOM.Common.Converter |
18 |
{ |
19 |
public class SvgImageConverter : SvgImageBase, IValueConverter |
20 |
{ |
21 |
#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 |
|
33 |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
34 |
{ |
35 |
ImageSource imageSource = null; |
36 |
|
37 |
try |
38 |
{ |
39 |
if (string.IsNullOrWhiteSpace(_appName)) |
40 |
{ |
41 |
if (this.IsDesignMode() || LicenseManager.UsageMode == LicenseUsageMode.Designtime) |
42 |
{ |
43 |
this.GetAppName(); |
44 |
} |
45 |
} |
46 |
|
47 |
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 |
|
65 |
if (inputUri == null) |
66 |
{ |
67 |
return null; |
68 |
} |
69 |
var svgSource = inputUri.IsAbsoluteUri ? inputUri : new Uri(_baseUri, inputUri); |
70 |
|
71 |
if (svgSource.ToString().EndsWith(".svg")) |
72 |
{ |
73 |
//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 |
} |
92 |
else |
93 |
{ |
94 |
imageSource = new BitmapImage(new Uri(value.ToString())); |
95 |
} |
96 |
|
97 |
} |
98 |
catch |
99 |
{ |
100 |
//throw; #82 |
101 |
} |
102 |
|
103 |
return imageSource; |
104 |
} |
105 |
|
106 |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
107 |
{ |
108 |
throw new NotImplementedException(); |
109 |
} |
110 |
|
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 |
} |
236 |
} |