1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.IO.Compression;
|
---|
4 | using System.Xml;
|
---|
5 | using System.Text;
|
---|
6 |
|
---|
7 | using System.Windows;
|
---|
8 | using System.Windows.Media;
|
---|
9 | using System.Windows.Media.Effects;
|
---|
10 | using System.Windows.Media.Imaging;
|
---|
11 |
|
---|
12 | using SharpVectors.Dom.Svg;
|
---|
13 | using SharpVectors.Runtime;
|
---|
14 | using SharpVectors.Renderers;
|
---|
15 | using SharpVectors.Renderers.Wpf;
|
---|
16 | using SharpVectors.Renderers.Utils;
|
---|
17 |
|
---|
18 | namespace SharpVectors.Converters
|
---|
19 | {
|
---|
20 | /// <summary>
|
---|
21 | /// This converts the SVG file to static or bitmap image, which is
|
---|
22 | /// saved to a file.
|
---|
23 | /// </summary>
|
---|
24 | /// <remarks>
|
---|
25 | /// <para>
|
---|
26 | /// The image is save with the <see cref="PixelFormats.Pbgra32"/> format,
|
---|
27 | /// since that is the only pixel format which does not throw an exception
|
---|
28 | /// with the <see cref="RenderTargetBitmap"/>.
|
---|
29 | /// </para>
|
---|
30 | /// <para>
|
---|
31 | /// The DPI used is 96.
|
---|
32 | /// </para>
|
---|
33 | /// </remarks>
|
---|
34 | public sealed class ImageSvgConverter : SvgConverter
|
---|
35 | {
|
---|
36 | #region Private Fields
|
---|
37 |
|
---|
38 | private bool _writerErrorOccurred;
|
---|
39 | private bool _fallbackOnWriterError;
|
---|
40 |
|
---|
41 | private ImageEncoderType _encoderType;
|
---|
42 | private BitmapEncoder _bitampEncoder;
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Required designer variable.
|
---|
46 | /// </summary>
|
---|
47 | private WpfSvgWindow _wpfWindow;
|
---|
48 | private WpfDrawingRenderer _wpfRenderer;
|
---|
49 |
|
---|
50 | #endregion
|
---|
51 |
|
---|
52 | #region Constructors and Destructor
|
---|
53 |
|
---|
54 | /// <overloads>
|
---|
55 | /// Initializes a new instance of the <see cref="ImageSvgConverter"/> class.
|
---|
56 | /// </overloads>
|
---|
57 | /// <summary>
|
---|
58 | /// Initializes a new instance of the <see cref="ImageSvgConverter"/> class
|
---|
59 | /// with the specified drawing or rendering settings.
|
---|
60 | /// </summary>
|
---|
61 | /// <param name="settings">
|
---|
62 | /// This specifies the settings used by the rendering or drawing engine.
|
---|
63 | /// If this is <see langword="null"/>, the default settings is used.
|
---|
64 | /// </param>
|
---|
65 | public ImageSvgConverter(WpfDrawingSettings settings)
|
---|
66 | : this(false, false, settings)
|
---|
67 | {
|
---|
68 | }
|
---|
69 |
|
---|
70 | /// <summary>
|
---|
71 | /// Initializes a new instance of the <see cref="ImageSvgConverter"/> class
|
---|
72 | /// with the specified drawing or rendering settings and the saving options.
|
---|
73 | /// </summary>
|
---|
74 | /// <param name="saveXaml">
|
---|
75 | /// This specifies whether to save result object tree in image file.
|
---|
76 | /// </param>
|
---|
77 | /// <param name="saveZaml">
|
---|
78 | /// This specifies whether to save result object tree in ZAML file. The
|
---|
79 | /// ZAML is simply a G-Zip compressed image format, similar to the SVGZ.
|
---|
80 | /// </param>
|
---|
81 | /// <param name="settings">
|
---|
82 | /// This specifies the settings used by the rendering or drawing engine.
|
---|
83 | /// If this is <see langword="null"/>, the default settings is used.
|
---|
84 | /// </param>
|
---|
85 | public ImageSvgConverter(bool saveXaml, bool saveZaml,
|
---|
86 | WpfDrawingSettings settings)
|
---|
87 | : base(saveXaml, saveZaml, settings)
|
---|
88 | {
|
---|
89 | _encoderType = ImageEncoderType.PngBitmap;
|
---|
90 |
|
---|
91 | _wpfRenderer = new WpfDrawingRenderer(this.DrawingSettings);
|
---|
92 | _wpfWindow = new WpfSvgWindow(640, 480, _wpfRenderer);
|
---|
93 | }
|
---|
94 |
|
---|
95 | #endregion
|
---|
96 |
|
---|
97 | #region Public Properties
|
---|
98 |
|
---|
99 | /// <summary>
|
---|
100 | /// Gets a value indicating whether a writer error occurred when
|
---|
101 | /// using the custom image writer.
|
---|
102 | /// </summary>
|
---|
103 | /// <value>
|
---|
104 | /// This is <see langword="true"/> if an error occurred when using
|
---|
105 | /// the custom image writer; otherwise, it is <see langword="false"/>.
|
---|
106 | /// </value>
|
---|
107 | public bool WriterErrorOccurred
|
---|
108 | {
|
---|
109 | get
|
---|
110 | {
|
---|
111 | return _writerErrorOccurred;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /// <summary>
|
---|
116 | /// Gets or sets a value indicating whether to fall back and use
|
---|
117 | /// the .NET Framework image writer when an error occurred in using the
|
---|
118 | /// custom writer.
|
---|
119 | /// </summary>
|
---|
120 | /// <value>
|
---|
121 | /// This is <see langword="true"/> if the converter falls back to using
|
---|
122 | /// the system image writer when an error occurred in using the custom
|
---|
123 | /// writer; otherwise, it is <see langword="false"/>. If <see langword="false"/>,
|
---|
124 | /// an exception, which occurred in using the custom writer will be
|
---|
125 | /// thrown. The default is <see langword="false"/>.
|
---|
126 | /// </value>
|
---|
127 | public bool FallbackOnWriterError
|
---|
128 | {
|
---|
129 | get
|
---|
130 | {
|
---|
131 | return _fallbackOnWriterError;
|
---|
132 | }
|
---|
133 | set
|
---|
134 | {
|
---|
135 | _fallbackOnWriterError = value;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /// <summary>
|
---|
140 | /// Gets or set the bitmap encoder type to use in encoding the drawing
|
---|
141 | /// to an image file.
|
---|
142 | /// </summary>
|
---|
143 | /// <value>
|
---|
144 | /// An enumeration of the type <see cref="ImageEncoderType"/> specifying
|
---|
145 | /// the bitmap encoder. The default is the <see cref="ImageEncoderType.PngBitmap"/>.
|
---|
146 | /// </value>
|
---|
147 | public ImageEncoderType EncoderType
|
---|
148 | {
|
---|
149 | get
|
---|
150 | {
|
---|
151 | return _encoderType;
|
---|
152 | }
|
---|
153 | set
|
---|
154 | {
|
---|
155 | _encoderType = value;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | /// <summary>
|
---|
160 | /// Gets or sets a custom bitmap encoder to use in encoding the drawing
|
---|
161 | /// to an image file.
|
---|
162 | /// </summary>
|
---|
163 | /// <value>
|
---|
164 | /// A derived <see cref="BitmapEncoder"/> object specifying the bitmap
|
---|
165 | /// encoder for encoding the images. The default is <see langword="null"/>,
|
---|
166 | /// and the <see cref="EncoderType"/> property determines the encoder used.
|
---|
167 | /// </value>
|
---|
168 | /// <remarks>
|
---|
169 | /// If the value of this is set, it must match the MIME type or file
|
---|
170 | /// extension defined by the <see cref="EncoderType"/> property for it
|
---|
171 | /// to be used.
|
---|
172 | /// </remarks>
|
---|
173 | public BitmapEncoder Encoder
|
---|
174 | {
|
---|
175 | get
|
---|
176 | {
|
---|
177 | return _bitampEncoder;
|
---|
178 | }
|
---|
179 | set
|
---|
180 | {
|
---|
181 | _bitampEncoder = value;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | #endregion
|
---|
186 |
|
---|
187 | #region Public Methods
|
---|
188 |
|
---|
189 | /// <overloads>
|
---|
190 | /// This performs the conversion of the specified SVG file, and saves
|
---|
191 | /// the output to an image file.
|
---|
192 | /// </overloads>
|
---|
193 | /// <summary>
|
---|
194 | /// This performs the conversion of the specified SVG file, and saves
|
---|
195 | /// the output to an image file with the same file name.
|
---|
196 | /// </summary>
|
---|
197 | /// <param name="svgFileName">
|
---|
198 | /// The full path of the SVG source file.
|
---|
199 | /// </param>
|
---|
200 | /// <returns>
|
---|
201 | /// This returns <see langword="true"/> if the conversion is successful;
|
---|
202 | /// otherwise, it return <see langword="false"/>.
|
---|
203 | /// </returns>
|
---|
204 | /// <exception cref="ArgumentNullException">
|
---|
205 | /// If the <paramref name="svgFileName"/> is <see langword="null"/>.
|
---|
206 | /// </exception>
|
---|
207 | /// <exception cref="ArgumentException">
|
---|
208 | /// If the <paramref name="svgFileName"/> is empty.
|
---|
209 | /// <para>-or-</para>
|
---|
210 | /// If the <paramref name="svgFileName"/> does not exists.
|
---|
211 | /// </exception>
|
---|
212 | public bool Convert(string svgFileName)
|
---|
213 | {
|
---|
214 | return this.Convert(svgFileName, String.Empty);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /// <summary>
|
---|
218 | /// This performs the conversion of the specified SVG file, and saves
|
---|
219 | /// the output to the specified image file.
|
---|
220 | /// </summary>
|
---|
221 | /// <param name="svgFileName">
|
---|
222 | /// The full path of the SVG source file.
|
---|
223 | /// </param>
|
---|
224 | /// <param name="imageFileName">
|
---|
225 | /// The output image file. This is optional. If not specified, an image
|
---|
226 | /// file is created in the same directory as the SVG file.
|
---|
227 | /// </param>
|
---|
228 | /// <returns>
|
---|
229 | /// This returns <see langword="true"/> if the conversion is successful;
|
---|
230 | /// otherwise, it return <see langword="false"/>.
|
---|
231 | /// </returns>
|
---|
232 | /// <exception cref="ArgumentNullException">
|
---|
233 | /// If the <paramref name="svgFileName"/> is <see langword="null"/>.
|
---|
234 | /// </exception>
|
---|
235 | /// <exception cref="ArgumentException">
|
---|
236 | /// If the <paramref name="svgFileName"/> is empty.
|
---|
237 | /// <para>-or-</para>
|
---|
238 | /// If the <paramref name="svgFileName"/> does not exists.
|
---|
239 | /// </exception>
|
---|
240 | public bool Convert(string svgFileName, string imageFileName)
|
---|
241 | {
|
---|
242 | if (svgFileName == null)
|
---|
243 | {
|
---|
244 | throw new ArgumentNullException("svgFileName",
|
---|
245 | "The SVG source file cannot be null (or Nothing).");
|
---|
246 | }
|
---|
247 | if (svgFileName.Length == 0)
|
---|
248 | {
|
---|
249 | throw new ArgumentException(
|
---|
250 | "The SVG source file cannot be empty.", "svgFileName");
|
---|
251 | }
|
---|
252 | if (!File.Exists(svgFileName))
|
---|
253 | {
|
---|
254 | throw new ArgumentException(
|
---|
255 | "The SVG source file must exists.", "svgFileName");
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (String.IsNullOrEmpty(svgFileName) || !File.Exists(svgFileName))
|
---|
259 | {
|
---|
260 | return false;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (!String.IsNullOrEmpty(imageFileName))
|
---|
264 | {
|
---|
265 | string workingDir = Path.GetDirectoryName(imageFileName);
|
---|
266 | if (!Directory.Exists(workingDir))
|
---|
267 | {
|
---|
268 | Directory.CreateDirectory(workingDir);
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | return this.ProcessFile(svgFileName, imageFileName);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /// <summary>
|
---|
276 | /// This performs the conversion of the specified SVG source, and saves
|
---|
277 | /// the output to the specified image file.
|
---|
278 | /// </summary>
|
---|
279 | /// <param name="svgStream">
|
---|
280 | /// A stream providing access to the SVG source data.
|
---|
281 | /// </param>
|
---|
282 | /// <param name="imageFileName">
|
---|
283 | /// The output image file. This is optional. If not specified, an image
|
---|
284 | /// file is created in the same directory as the SVG file.
|
---|
285 | /// </param>
|
---|
286 | /// <returns>
|
---|
287 | /// This returns <see langword="true"/> if the conversion is successful;
|
---|
288 | /// otherwise, it return <see langword="false"/>.
|
---|
289 | /// </returns>
|
---|
290 | /// <exception cref="ArgumentNullException">
|
---|
291 | /// If the <paramref name="imageFileName"/> is <see langword="null"/>.
|
---|
292 | /// <para>-or-</para>
|
---|
293 | /// If the <paramref name="svgStream"/> is <see langword="null"/>.
|
---|
294 | /// </exception>
|
---|
295 | /// <exception cref="ArgumentException">
|
---|
296 | /// If the <paramref name="imageFileName"/> is empty.
|
---|
297 | /// </exception>
|
---|
298 | public bool Convert(Stream svgStream, string imageFileName)
|
---|
299 | {
|
---|
300 | if (svgStream == null)
|
---|
301 | {
|
---|
302 | throw new ArgumentNullException("svgStream",
|
---|
303 | "The SVG source file cannot be null (or Nothing).");
|
---|
304 | }
|
---|
305 | if (imageFileName == null)
|
---|
306 | {
|
---|
307 | throw new ArgumentNullException("imageFileName",
|
---|
308 | "The image destination file path cannot be null (or Nothing).");
|
---|
309 | }
|
---|
310 | if (imageFileName.Length == 0)
|
---|
311 | {
|
---|
312 | throw new ArgumentException(
|
---|
313 | "The image destination file path cannot be empty.", "imageFileName");
|
---|
314 | }
|
---|
315 |
|
---|
316 | if (!this.SaveXaml && !this.SaveZaml)
|
---|
317 | {
|
---|
318 | return false;
|
---|
319 | }
|
---|
320 |
|
---|
321 | if (!String.IsNullOrEmpty(imageFileName))
|
---|
322 | {
|
---|
323 | string workingDir = Path.GetDirectoryName(imageFileName);
|
---|
324 | if (!Directory.Exists(workingDir))
|
---|
325 | {
|
---|
326 | Directory.CreateDirectory(workingDir);
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | return this.ProcessFile(svgStream, imageFileName);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /// <summary>
|
---|
334 | /// This performs the conversion of the specified SVG source, and saves
|
---|
335 | /// the output to the specified image file.
|
---|
336 | /// </summary>
|
---|
337 | /// <param name="svgTextReader">
|
---|
338 | /// A text reader providing access to the SVG source data.
|
---|
339 | /// </param>
|
---|
340 | /// <param name="imageFileName">
|
---|
341 | /// The output image file. This is optional. If not specified, an image
|
---|
342 | /// file is created in the same directory as the SVG file.
|
---|
343 | /// </param>
|
---|
344 | /// <returns>
|
---|
345 | /// This returns <see langword="true"/> if the conversion is successful;
|
---|
346 | /// otherwise, it return <see langword="false"/>.
|
---|
347 | /// </returns>
|
---|
348 | /// <exception cref="ArgumentNullException">
|
---|
349 | /// If the <paramref name="imageFileName"/> is <see langword="null"/>.
|
---|
350 | /// <para>-or-</para>
|
---|
351 | /// If the <paramref name="svgTextReader"/> is <see langword="null"/>.
|
---|
352 | /// </exception>
|
---|
353 | /// <exception cref="ArgumentException">
|
---|
354 | /// If the <paramref name="imageFileName"/> is empty.
|
---|
355 | /// </exception>
|
---|
356 | public bool Convert(TextReader svgTextReader, string imageFileName)
|
---|
357 | {
|
---|
358 | if (svgTextReader == null)
|
---|
359 | {
|
---|
360 | throw new ArgumentNullException("svgTextReader",
|
---|
361 | "The SVG source file cannot be null (or Nothing).");
|
---|
362 | }
|
---|
363 | if (imageFileName == null)
|
---|
364 | {
|
---|
365 | throw new ArgumentNullException("imageFileName",
|
---|
366 | "The image destination file path cannot be null (or Nothing).");
|
---|
367 | }
|
---|
368 | if (imageFileName.Length == 0)
|
---|
369 | {
|
---|
370 | throw new ArgumentException(
|
---|
371 | "The image destination file path cannot be empty.", "imageFileName");
|
---|
372 | }
|
---|
373 |
|
---|
374 | if (!this.SaveXaml && !this.SaveZaml)
|
---|
375 | {
|
---|
376 | return false;
|
---|
377 | }
|
---|
378 |
|
---|
379 | if (!String.IsNullOrEmpty(imageFileName))
|
---|
380 | {
|
---|
381 | string workingDir = Path.GetDirectoryName(imageFileName);
|
---|
382 | if (!Directory.Exists(workingDir))
|
---|
383 | {
|
---|
384 | Directory.CreateDirectory(workingDir);
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | return this.ProcessFile(svgTextReader, imageFileName);
|
---|
389 | }
|
---|
390 |
|
---|
391 | /// <summary>
|
---|
392 | /// This performs the conversion of the specified SVG source, and saves
|
---|
393 | /// the output to the specified image file.
|
---|
394 | /// </summary>
|
---|
395 | /// <param name="svgXmlReader">
|
---|
396 | /// An XML reader providing access to the SVG source data.
|
---|
397 | /// </param>
|
---|
398 | /// <param name="imageFileName">
|
---|
399 | /// The output image file. This is optional. If not specified, an image
|
---|
400 | /// file is created in the same directory as the SVG file.
|
---|
401 | /// </param>
|
---|
402 | /// <returns>
|
---|
403 | /// This returns <see langword="true"/> if the conversion is successful;
|
---|
404 | /// otherwise, it return <see langword="false"/>.
|
---|
405 | /// </returns>
|
---|
406 | /// <exception cref="ArgumentNullException">
|
---|
407 | /// If the <paramref name="imageFileName"/> is <see langword="null"/>.
|
---|
408 | /// <para>-or-</para>
|
---|
409 | /// If the <paramref name="svgXmlReader"/> is <see langword="null"/>.
|
---|
410 | /// </exception>
|
---|
411 | /// <exception cref="ArgumentException">
|
---|
412 | /// If the <paramref name="imageFileName"/> is empty.
|
---|
413 | /// </exception>
|
---|
414 | public bool Convert(XmlReader svgXmlReader, string imageFileName)
|
---|
415 | {
|
---|
416 | if (svgXmlReader == null)
|
---|
417 | {
|
---|
418 | throw new ArgumentNullException("svgXmlReader",
|
---|
419 | "The SVG source file cannot be null (or Nothing).");
|
---|
420 | }
|
---|
421 | if (imageFileName == null)
|
---|
422 | {
|
---|
423 | throw new ArgumentNullException("imageFileName",
|
---|
424 | "The image destination file path cannot be null (or Nothing).");
|
---|
425 | }
|
---|
426 | if (imageFileName.Length == 0)
|
---|
427 | {
|
---|
428 | throw new ArgumentException(
|
---|
429 | "The image destination file path cannot be empty.", "imageFileName");
|
---|
430 | }
|
---|
431 |
|
---|
432 | if (!this.SaveXaml && !this.SaveZaml)
|
---|
433 | {
|
---|
434 | return false;
|
---|
435 | }
|
---|
436 |
|
---|
437 | if (!String.IsNullOrEmpty(imageFileName))
|
---|
438 | {
|
---|
439 | string workingDir = Path.GetDirectoryName(imageFileName);
|
---|
440 | if (!Directory.Exists(workingDir))
|
---|
441 | {
|
---|
442 | Directory.CreateDirectory(workingDir);
|
---|
443 | }
|
---|
444 | }
|
---|
445 |
|
---|
446 | return this.ProcessFile(svgXmlReader, imageFileName);
|
---|
447 | }
|
---|
448 |
|
---|
449 | #endregion
|
---|
450 |
|
---|
451 | #region Private Methods
|
---|
452 |
|
---|
453 | #region ProcessFile Method
|
---|
454 |
|
---|
455 | private bool ProcessFile(string fileName, string imageFileName)
|
---|
456 | {
|
---|
457 | _wpfRenderer.LinkVisitor = new LinkVisitor();
|
---|
458 | _wpfRenderer.ImageVisitor = new EmbeddedImageVisitor();
|
---|
459 | _wpfRenderer.FontFamilyVisitor = new FontFamilyVisitor();
|
---|
460 |
|
---|
461 | _wpfWindow.LoadDocument(fileName);
|
---|
462 |
|
---|
463 | _wpfRenderer.InvalidRect = SvgRectF.Empty;
|
---|
464 |
|
---|
465 | _wpfRenderer.Render(_wpfWindow.Document as SvgDocument);
|
---|
466 |
|
---|
467 | DrawingGroup renderedDrawing = _wpfRenderer.Drawing as DrawingGroup;
|
---|
468 | if (renderedDrawing == null)
|
---|
469 | {
|
---|
470 | return false;
|
---|
471 | }
|
---|
472 |
|
---|
473 | // Save to the image file...
|
---|
474 | SaveImageFile(renderedDrawing, fileName, imageFileName);
|
---|
475 |
|
---|
476 | // Save to image and/or ZAML file if required...
|
---|
477 | if (this.SaveXaml || this.SaveZaml)
|
---|
478 | {
|
---|
479 | SaveXamlFile(renderedDrawing, fileName, imageFileName);
|
---|
480 | }
|
---|
481 |
|
---|
482 | renderedDrawing = null;
|
---|
483 |
|
---|
484 | return true;
|
---|
485 | }
|
---|
486 |
|
---|
487 | private bool ProcessFile(Stream svgStream, string imageFileName)
|
---|
488 | {
|
---|
489 | _wpfRenderer.LinkVisitor = new LinkVisitor();
|
---|
490 | _wpfRenderer.ImageVisitor = new EmbeddedImageVisitor();
|
---|
491 | _wpfRenderer.FontFamilyVisitor = new FontFamilyVisitor();
|
---|
492 |
|
---|
493 | _wpfWindow.LoadDocument(svgStream);
|
---|
494 |
|
---|
495 | _wpfRenderer.InvalidRect = SvgRectF.Empty;
|
---|
496 |
|
---|
497 | _wpfRenderer.Render(_wpfWindow.Document as SvgDocument);
|
---|
498 |
|
---|
499 | DrawingGroup renderedDrawing = _wpfRenderer.Drawing as DrawingGroup;
|
---|
500 | if (renderedDrawing == null)
|
---|
501 | {
|
---|
502 | return false;
|
---|
503 | }
|
---|
504 |
|
---|
505 | // Save to the image file...
|
---|
506 | SaveImageFile(renderedDrawing, imageFileName, imageFileName);
|
---|
507 |
|
---|
508 | // Save to image and/or ZAML file if required...
|
---|
509 | if (this.SaveXaml || this.SaveZaml)
|
---|
510 | {
|
---|
511 | SaveXamlFile(renderedDrawing, imageFileName, imageFileName);
|
---|
512 | }
|
---|
513 |
|
---|
514 | renderedDrawing = null;
|
---|
515 |
|
---|
516 | return true;
|
---|
517 | }
|
---|
518 |
|
---|
519 | private bool ProcessFile(TextReader svgTextReader, string imageFileName)
|
---|
520 | {
|
---|
521 | _wpfRenderer.LinkVisitor = new LinkVisitor();
|
---|
522 | _wpfRenderer.ImageVisitor = new EmbeddedImageVisitor();
|
---|
523 | _wpfRenderer.FontFamilyVisitor = new FontFamilyVisitor();
|
---|
524 |
|
---|
525 | _wpfWindow.LoadDocument(svgTextReader);
|
---|
526 |
|
---|
527 | _wpfRenderer.InvalidRect = SvgRectF.Empty;
|
---|
528 |
|
---|
529 | _wpfRenderer.Render(_wpfWindow.Document as SvgDocument);
|
---|
530 |
|
---|
531 | DrawingGroup renderedDrawing = _wpfRenderer.Drawing as DrawingGroup;
|
---|
532 | if (renderedDrawing == null)
|
---|
533 | {
|
---|
534 | return false;
|
---|
535 | }
|
---|
536 |
|
---|
537 | // Save to the image file...
|
---|
538 | SaveImageFile(renderedDrawing, imageFileName, imageFileName);
|
---|
539 |
|
---|
540 | // Save to image and/or ZAML file if required...
|
---|
541 | if (this.SaveXaml || this.SaveZaml)
|
---|
542 | {
|
---|
543 | SaveXamlFile(renderedDrawing, imageFileName, imageFileName);
|
---|
544 | }
|
---|
545 |
|
---|
546 | renderedDrawing = null;
|
---|
547 |
|
---|
548 | return true;
|
---|
549 | }
|
---|
550 |
|
---|
551 | private bool ProcessFile(XmlReader svgXmlReader, string imageFileName)
|
---|
552 | {
|
---|
553 | _wpfRenderer.LinkVisitor = new LinkVisitor();
|
---|
554 | _wpfRenderer.ImageVisitor = new EmbeddedImageVisitor();
|
---|
555 | _wpfRenderer.FontFamilyVisitor = new FontFamilyVisitor();
|
---|
556 |
|
---|
557 | _wpfWindow.LoadDocument(svgXmlReader);
|
---|
558 |
|
---|
559 | _wpfRenderer.InvalidRect = SvgRectF.Empty;
|
---|
560 |
|
---|
561 | _wpfRenderer.Render(_wpfWindow.Document as SvgDocument);
|
---|
562 |
|
---|
563 | DrawingGroup renderedDrawing = _wpfRenderer.Drawing as DrawingGroup;
|
---|
564 | if (renderedDrawing == null)
|
---|
565 | {
|
---|
566 | return false;
|
---|
567 | }
|
---|
568 |
|
---|
569 | // Save to the image file...
|
---|
570 | SaveImageFile(renderedDrawing, imageFileName, imageFileName);
|
---|
571 |
|
---|
572 | // Save to image and/or ZAML file if required...
|
---|
573 | if (this.SaveXaml || this.SaveZaml)
|
---|
574 | {
|
---|
575 | SaveXamlFile(renderedDrawing, imageFileName, imageFileName);
|
---|
576 | }
|
---|
577 |
|
---|
578 | renderedDrawing = null;
|
---|
579 |
|
---|
580 | return true;
|
---|
581 | }
|
---|
582 |
|
---|
583 | #endregion
|
---|
584 |
|
---|
585 | #region SaveImageFile Method
|
---|
586 |
|
---|
587 | private bool SaveImageFile(Drawing drawing, string fileName,
|
---|
588 | string imageFileName)
|
---|
589 | {
|
---|
590 | string outputExt = this.GetImageFileExtention();
|
---|
591 | string outputFileName = null;
|
---|
592 | if (String.IsNullOrEmpty(imageFileName))
|
---|
593 | {
|
---|
594 | string fileNameWithoutExt =
|
---|
595 | Path.GetFileNameWithoutExtension(fileName);
|
---|
596 |
|
---|
597 | string workingDir = Path.GetDirectoryName(fileName);
|
---|
598 | outputFileName = Path.Combine(workingDir,
|
---|
599 | fileNameWithoutExt + outputExt);
|
---|
600 | }
|
---|
601 | else
|
---|
602 | {
|
---|
603 | string fileExt = Path.GetExtension(imageFileName);
|
---|
604 | if (String.IsNullOrEmpty(fileExt))
|
---|
605 | {
|
---|
606 | outputFileName = imageFileName + outputExt;
|
---|
607 | }
|
---|
608 | else if (!String.Equals(fileExt, outputExt,
|
---|
609 | StringComparison.OrdinalIgnoreCase))
|
---|
610 | {
|
---|
611 | outputFileName = Path.ChangeExtension(imageFileName, outputExt);
|
---|
612 | }
|
---|
613 | }
|
---|
614 |
|
---|
615 | BitmapEncoder bitampEncoder = this.GetBitmapEncoder(outputExt);
|
---|
616 |
|
---|
617 | // The image parameters...
|
---|
618 | Rect drawingBounds = drawing.Bounds;
|
---|
619 | int pixelWidth = (int)drawingBounds.Width;
|
---|
620 | int pixelHeight = (int)drawingBounds.Height;
|
---|
621 | double dpiX = 96;
|
---|
622 | double dpiY = 96;
|
---|
623 |
|
---|
624 | // The Visual to use as the source of the RenderTargetBitmap.
|
---|
625 | DrawingVisualEx drawingVisual = new DrawingVisualEx();
|
---|
626 | DrawingContext drawingContext = drawingVisual.RenderOpen();
|
---|
627 | drawingContext.DrawDrawing(drawing);
|
---|
628 | drawingContext.Close();
|
---|
629 |
|
---|
630 | // The BitmapSource that is rendered with a Visual.
|
---|
631 | RenderTargetBitmap targetBitmap = new RenderTargetBitmap(
|
---|
632 | pixelWidth, pixelHeight, dpiX, dpiY, PixelFormats.Pbgra32);
|
---|
633 | targetBitmap.Render(drawingVisual);
|
---|
634 |
|
---|
635 | // Encoding the RenderBitmapTarget as an image file.
|
---|
636 | bitampEncoder.Frames.Add(BitmapFrame.Create(targetBitmap));
|
---|
637 | using (FileStream stream = File.Create(outputFileName))
|
---|
638 | {
|
---|
639 | bitampEncoder.Save(stream);
|
---|
640 | }
|
---|
641 |
|
---|
642 | return true;
|
---|
643 | }
|
---|
644 |
|
---|
645 | private BitmapEncoder GetBitmapEncoder(string fileExtension)
|
---|
646 | {
|
---|
647 | BitmapEncoder bitampEncoder = null;
|
---|
648 |
|
---|
649 | if (_bitampEncoder != null && _bitampEncoder.CodecInfo != null)
|
---|
650 | {
|
---|
651 | string mimeType = String.Empty;
|
---|
652 | BitmapCodecInfo codecInfo = _bitampEncoder.CodecInfo;
|
---|
653 | string mimeTypes = codecInfo.MimeTypes;
|
---|
654 | string fileExtensions = codecInfo.FileExtensions;
|
---|
655 | switch (_encoderType)
|
---|
656 | {
|
---|
657 | case ImageEncoderType.BmpBitmap:
|
---|
658 | mimeType = "image/bmp";
|
---|
659 | break;
|
---|
660 | case ImageEncoderType.GifBitmap:
|
---|
661 | mimeType = "image/gif";
|
---|
662 | break;
|
---|
663 | case ImageEncoderType.JpegBitmap:
|
---|
664 | mimeType = "image/jpeg,image/jpe,image/jpg";
|
---|
665 | break;
|
---|
666 | case ImageEncoderType.PngBitmap:
|
---|
667 | mimeType = "image/png";
|
---|
668 | break;
|
---|
669 | case ImageEncoderType.TiffBitmap:
|
---|
670 | mimeType = "image/tiff,image/tif";
|
---|
671 | break;
|
---|
672 | case ImageEncoderType.WmpBitmap:
|
---|
673 | mimeType = "image/vnd.ms-photo";
|
---|
674 | break;
|
---|
675 | }
|
---|
676 |
|
---|
677 | if (!String.IsNullOrEmpty(fileExtensions) &&
|
---|
678 | fileExtensions.IndexOf(fileExtension,
|
---|
679 | StringComparison.OrdinalIgnoreCase) >= 0)
|
---|
680 | {
|
---|
681 | bitampEncoder = _bitampEncoder;
|
---|
682 | }
|
---|
683 | else if (!String.IsNullOrEmpty(mimeTypes) &&
|
---|
684 | !String.IsNullOrEmpty(mimeType))
|
---|
685 | {
|
---|
686 | string[] arrayMimeTypes = mimeType.Split(',');
|
---|
687 | for (int i = 0; i < arrayMimeTypes.Length; i++)
|
---|
688 | {
|
---|
689 | if (mimeTypes.IndexOf(arrayMimeTypes[i],
|
---|
690 | StringComparison.OrdinalIgnoreCase) >= 0)
|
---|
691 | {
|
---|
692 | bitampEncoder = _bitampEncoder;
|
---|
693 | break;
|
---|
694 | }
|
---|
695 | }
|
---|
696 | }
|
---|
697 | }
|
---|
698 |
|
---|
699 | if (bitampEncoder == null)
|
---|
700 | {
|
---|
701 | switch (_encoderType)
|
---|
702 | {
|
---|
703 | case ImageEncoderType.BmpBitmap:
|
---|
704 | bitampEncoder = new BmpBitmapEncoder();
|
---|
705 | break;
|
---|
706 | case ImageEncoderType.GifBitmap:
|
---|
707 | bitampEncoder = new GifBitmapEncoder();
|
---|
708 | break;
|
---|
709 | case ImageEncoderType.JpegBitmap:
|
---|
710 | JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
|
---|
711 | // Set the default/user options...
|
---|
712 | bitampEncoder = jpgEncoder;
|
---|
713 | break;
|
---|
714 | case ImageEncoderType.PngBitmap:
|
---|
715 | PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
|
---|
716 | // Set the default/user options...
|
---|
717 | bitampEncoder = pngEncoder;
|
---|
718 | break;
|
---|
719 | case ImageEncoderType.TiffBitmap:
|
---|
720 | bitampEncoder = new TiffBitmapEncoder();
|
---|
721 | break;
|
---|
722 | case ImageEncoderType.WmpBitmap:
|
---|
723 | WmpBitmapEncoder wmpEncoder = new WmpBitmapEncoder();
|
---|
724 | // Set the default/user options...
|
---|
725 | bitampEncoder = wmpEncoder;
|
---|
726 | break;
|
---|
727 | }
|
---|
728 | }
|
---|
729 |
|
---|
730 | if (bitampEncoder == null)
|
---|
731 | {
|
---|
732 | bitampEncoder = new PngBitmapEncoder();
|
---|
733 | }
|
---|
734 |
|
---|
735 | return bitampEncoder;
|
---|
736 | }
|
---|
737 |
|
---|
738 | private string GetImageFileExtention()
|
---|
739 | {
|
---|
740 | switch (_encoderType)
|
---|
741 | {
|
---|
742 | case ImageEncoderType.BmpBitmap:
|
---|
743 | return ".bmp";
|
---|
744 | case ImageEncoderType.GifBitmap:
|
---|
745 | return ".gif";
|
---|
746 | case ImageEncoderType.JpegBitmap:
|
---|
747 | return ".jpg";
|
---|
748 | case ImageEncoderType.PngBitmap:
|
---|
749 | return ".png";
|
---|
750 | case ImageEncoderType.TiffBitmap:
|
---|
751 | return ".tif";
|
---|
752 | case ImageEncoderType.WmpBitmap:
|
---|
753 | return ".wdp";
|
---|
754 | }
|
---|
755 |
|
---|
756 | return ".png";
|
---|
757 | }
|
---|
758 |
|
---|
759 | #endregion
|
---|
760 |
|
---|
761 | #region SaveXamlFile Method
|
---|
762 |
|
---|
763 | private bool SaveXamlFile(Drawing drawing, string fileName, string imageFileName)
|
---|
764 | {
|
---|
765 | _writerErrorOccurred = false;
|
---|
766 |
|
---|
767 | string xamlFileName = null;
|
---|
768 | if (String.IsNullOrEmpty(imageFileName))
|
---|
769 | {
|
---|
770 | string fileNameWithoutExt =
|
---|
771 | Path.GetFileNameWithoutExtension(fileName);
|
---|
772 |
|
---|
773 | string workingDir = Path.GetDirectoryName(fileName);
|
---|
774 | xamlFileName = Path.Combine(workingDir,
|
---|
775 | fileNameWithoutExt + ".xaml");
|
---|
776 | }
|
---|
777 | else
|
---|
778 | {
|
---|
779 | string fileExt = Path.GetExtension(imageFileName);
|
---|
780 | if (String.IsNullOrEmpty(fileExt))
|
---|
781 | {
|
---|
782 | xamlFileName = imageFileName + ".xaml";
|
---|
783 | }
|
---|
784 | else if (!String.Equals(fileExt, ".xaml",
|
---|
785 | StringComparison.OrdinalIgnoreCase))
|
---|
786 | {
|
---|
787 | xamlFileName = Path.ChangeExtension(imageFileName, ".xaml");
|
---|
788 | }
|
---|
789 | }
|
---|
790 |
|
---|
791 | if (File.Exists(xamlFileName))
|
---|
792 | {
|
---|
793 | File.SetAttributes(xamlFileName, FileAttributes.Normal);
|
---|
794 | File.Delete(xamlFileName);
|
---|
795 | }
|
---|
796 |
|
---|
797 | if (this.UseFrameXamlWriter)
|
---|
798 | {
|
---|
799 | XmlWriterSettings writerSettings = new XmlWriterSettings();
|
---|
800 | writerSettings.Indent = true;
|
---|
801 | writerSettings.OmitXmlDeclaration = true;
|
---|
802 | writerSettings.Encoding = Encoding.UTF8;
|
---|
803 | using (FileStream xamlFile = File.Create(xamlFileName))
|
---|
804 | {
|
---|
805 | using (XmlWriter writer = XmlWriter.Create(
|
---|
806 | xamlFile, writerSettings))
|
---|
807 | {
|
---|
808 | System.Windows.Markup.XamlWriter.Save(
|
---|
809 | drawing, writer);
|
---|
810 | }
|
---|
811 | }
|
---|
812 | }
|
---|
813 | else
|
---|
814 | {
|
---|
815 | try
|
---|
816 | {
|
---|
817 | XmlXamlWriter xamlWriter = new XmlXamlWriter(
|
---|
818 | this.DrawingSettings);
|
---|
819 |
|
---|
820 | using (FileStream xamlFile = File.Create(xamlFileName))
|
---|
821 | {
|
---|
822 | xamlWriter.Save(drawing, xamlFile);
|
---|
823 | }
|
---|
824 | }
|
---|
825 | catch
|
---|
826 | {
|
---|
827 | _writerErrorOccurred = true;
|
---|
828 |
|
---|
829 | if (_fallbackOnWriterError)
|
---|
830 | {
|
---|
831 | if (File.Exists(xamlFileName))
|
---|
832 | {
|
---|
833 | File.Move(xamlFileName, xamlFileName + ".bak");
|
---|
834 | }
|
---|
835 |
|
---|
836 | XmlWriterSettings writerSettings = new XmlWriterSettings();
|
---|
837 | writerSettings.Indent = true;
|
---|
838 | writerSettings.OmitXmlDeclaration = true;
|
---|
839 | writerSettings.Encoding = Encoding.UTF8;
|
---|
840 | using (FileStream xamlFile = File.Create(xamlFileName))
|
---|
841 | {
|
---|
842 | using (XmlWriter writer = XmlWriter.Create(
|
---|
843 | xamlFile, writerSettings))
|
---|
844 | {
|
---|
845 | System.Windows.Markup.XamlWriter.Save(
|
---|
846 | drawing, writer);
|
---|
847 | }
|
---|
848 | }
|
---|
849 | }
|
---|
850 | else
|
---|
851 | {
|
---|
852 | throw;
|
---|
853 | }
|
---|
854 | }
|
---|
855 | }
|
---|
856 |
|
---|
857 | if (this.SaveZaml)
|
---|
858 | {
|
---|
859 | string zamlFileName = Path.ChangeExtension(xamlFileName, ".zaml");
|
---|
860 |
|
---|
861 | if (File.Exists(zamlFileName))
|
---|
862 | {
|
---|
863 | File.SetAttributes(zamlFileName, FileAttributes.Normal);
|
---|
864 | File.Delete(zamlFileName);
|
---|
865 | }
|
---|
866 |
|
---|
867 | FileStream zamlSourceFile = new FileStream(xamlFileName, FileMode.Open,
|
---|
868 | FileAccess.Read, FileShare.Read);
|
---|
869 | byte[] buffer = new byte[zamlSourceFile.Length];
|
---|
870 | // Read the file to ensure it is readable.
|
---|
871 | int count = zamlSourceFile.Read(buffer, 0, buffer.Length);
|
---|
872 | if (count != buffer.Length)
|
---|
873 | {
|
---|
874 | zamlSourceFile.Close();
|
---|
875 | return false;
|
---|
876 | }
|
---|
877 | zamlSourceFile.Close();
|
---|
878 |
|
---|
879 | FileStream zamlDestFile = File.Create(zamlFileName);
|
---|
880 |
|
---|
881 | GZipStream zipStream = new GZipStream(zamlDestFile, CompressionMode.Compress, true);
|
---|
882 | zipStream.Write(buffer, 0, buffer.Length);
|
---|
883 |
|
---|
884 | zipStream.Close();
|
---|
885 |
|
---|
886 | zamlDestFile.Close();
|
---|
887 | }
|
---|
888 |
|
---|
889 | if (!this.SaveXaml && File.Exists(xamlFileName))
|
---|
890 | {
|
---|
891 | File.Delete(xamlFileName);
|
---|
892 | }
|
---|
893 |
|
---|
894 | return true;
|
---|
895 | }
|
---|
896 |
|
---|
897 | #endregion
|
---|
898 |
|
---|
899 | #endregion
|
---|
900 |
|
---|
901 | #region DrawingVisualEx Class
|
---|
902 |
|
---|
903 | public sealed class DrawingVisualEx : DrawingVisual
|
---|
904 | {
|
---|
905 | public DrawingVisualEx()
|
---|
906 | {
|
---|
907 | }
|
---|
908 |
|
---|
909 | public Effect Effect
|
---|
910 | {
|
---|
911 | get
|
---|
912 | {
|
---|
913 | return this.VisualEffect;
|
---|
914 | }
|
---|
915 | set
|
---|
916 | {
|
---|
917 | this.VisualEffect = value;
|
---|
918 | }
|
---|
919 | }
|
---|
920 | }
|
---|
921 |
|
---|
922 | #endregion
|
---|
923 | }
|
---|
924 | }
|
---|