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