Esempio n. 1
0
        public override ImageSource Visit(SvgImageElement element, WpfDrawingContext context)
        {
            string sURI       = element.Href.AnimVal.Replace(" ", "");
            int    nColon     = sURI.IndexOf(":", StringComparison.OrdinalIgnoreCase);
            int    nSemiColon = sURI.IndexOf(";", StringComparison.OrdinalIgnoreCase);
            int    nComma     = sURI.IndexOf(",", StringComparison.OrdinalIgnoreCase);

            string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1);

            string sContent = SvgObject.RemoveWhitespace(sURI.Substring(nComma + 1));

            byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(),
                                                            0, sContent.Length);

            switch (sMimeType.Trim())
            {
            case "image/svg+xml":
                using (var stream = new MemoryStream(imageBytes))
                    using (var reader = new FileSvgReader(context.Settings))
                        return(new DrawingImage(reader.Read(stream)));

            default:
                return(new EmbeddedBitmapSource(new MemoryStream(imageBytes)));
            }
        }
        private ImageSource GetImage(SvgImageElement element, WpfDrawingContext context)
        {
            if (context != null && context.Settings.IncludeRuntime == false)
            {
                return(null);
            }

            var comparer = StringComparison.OrdinalIgnoreCase;

            string sURI       = element.Href.AnimVal.Replace(" ", string.Empty);
            int    nColon     = sURI.IndexOf(":", comparer);
            int    nSemiColon = sURI.IndexOf(";", comparer);
            int    nComma     = sURI.IndexOf(",", comparer);

            string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1);

            string sContent = SvgObject.RemoveWhitespace(sURI.Substring(nComma + 1));

            byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(), 0, sContent.Length);
            bool   isGZiped   = sContent.StartsWith(SvgConstants.GZipSignature, StringComparison.Ordinal);
            bool   isSvgOrXml = sContent.StartsWith(SvgConstants.SvgSignature, StringComparison.Ordinal) ||
                                sContent.StartsWith(SvgConstants.XmlSignature, StringComparison.Ordinal);

            if (string.Equals(sMimeType, "image/svg+xml", comparer) || isSvgOrXml)
            {
                if (isGZiped)
                {
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        using (GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress))
                        {
                            using (var reader = new FileSvgReader(context.Settings, true))
                            {
                                return(new DrawingImage(reader.Read(zipStream)));
                            }
                        }
                    }
                }
                else
                {
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        using (var reader = new FileSvgReader(context.Settings, true))
                        {
                            return(new DrawingImage(reader.Read(stream)));
                        }
                    }
                }
            }

            return(new EmbeddedBitmapSource(new MemoryStream(imageBytes)));
        }
Esempio n. 3
0
        private ImageSource GetImage(SvgImageElement element, WpfDrawingContext context)
        {
            bool   isSavingImages = _saveImages;
            string imagesDir      = _saveDirectory;

            if (context != null && context.Settings.IncludeRuntime == false)
            {
                isSavingImages = true;
                if (string.IsNullOrWhiteSpace(_saveDirectory) ||
                    Directory.Exists(_saveDirectory) == false)
                {
                    var assembly = Assembly.GetExecutingAssembly();
                    if (assembly != null)
                    {
                        imagesDir = Path.GetDirectoryName(assembly.Location);
                    }
                }
            }

            var comparer = StringComparison.OrdinalIgnoreCase;

            string sURI       = element.Href.AnimVal.Replace(" ", "");
            int    nColon     = sURI.IndexOf(":", comparer);
            int    nSemiColon = sURI.IndexOf(";", comparer);
            int    nComma     = sURI.IndexOf(",", comparer);

            string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1);

            string sContent = SvgObject.RemoveWhitespace(sURI.Substring(nComma + 1));

            byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(),
                                                            0, sContent.Length);
            bool isGZiped = sContent.StartsWith(SvgObject.GZipSignature, StringComparison.Ordinal);

            if (string.Equals(sMimeType, "image/svg+xml", comparer))
            {
                if (isGZiped)
                {
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        using (GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress))
                        {
                            using (var reader = new FileSvgReader(context.Settings))
                                return(new DrawingImage(reader.Read(zipStream)));
                        }
                    }
                }
                else
                {
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        using (var reader = new FileSvgReader(context.Settings))
                            return(new DrawingImage(reader.Read(stream)));
                    }
                }
            }

            BitmapImage imageSource = new BitmapImage();

            imageSource.BeginInit();
            imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
            imageSource.StreamSource  = new MemoryStream(imageBytes);
            imageSource.EndInit();

            imageSource.Freeze();

            if (isSavingImages && !string.IsNullOrWhiteSpace(imagesDir) &&
                Directory.Exists(imagesDir))
            {
                var imagePath = this.GetImagePath(imagesDir);

                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(imageSource));

                using (var fileStream = new FileStream(imagePath, FileMode.Create))
                {
                    encoder.Save(fileStream);
                }

                BitmapImage savedSource = new BitmapImage();

                savedSource.BeginInit();
                savedSource.CacheOption   = BitmapCacheOption.OnLoad;
                savedSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                savedSource.UriSource     = new Uri(imagePath);
                savedSource.EndInit();

                savedSource.Freeze();

                return(savedSource);
            }

            return(imageSource);
        }
Esempio n. 4
0
        /// <summary>
        /// This converts the SVG resource specified by the Uri to <see cref="DrawingGroup"/>.
        /// </summary>
        /// <param name="svgSource">A <see cref="Uri"/> specifying the source of the SVG resource.</param>
        /// <returns>A <see cref="DrawingGroup"/> of the converted SVG resource.</returns>
        protected virtual DrawingGroup GetDrawing(Uri svgSource)
        {
            string scheme = null;
            // A little hack to display preview in design mode: The design mode Uri is not absolute.
            bool designTime = DesignerProperties.GetIsInDesignMode(new DependencyObject());

            if (designTime && svgSource.IsAbsoluteUri == false)
            {
                scheme = "pack";
            }
            else
            {
                scheme = svgSource.Scheme;
            }
            if (string.IsNullOrWhiteSpace(scheme))
            {
                return(null);
            }

            WpfDrawingSettings settings = new WpfDrawingSettings();

            settings.IncludeRuntime = _includeRuntime;
            settings.TextAsGeometry = _textAsGeometry;
            settings.OptimizePath   = _optimizePath;
            if (_culture != null)
            {
                settings.CultureInfo = _culture;
            }

            switch (scheme)
            {
            case "file":
            //case "ftp":
            case "https":
            case "http":
                using (FileSvgReader reader = new FileSvgReader(settings))
                {
                    DrawingGroup drawGroup = reader.Read(svgSource);

                    if (drawGroup != null)
                    {
                        return(drawGroup);
                    }
                }
                break;

            case "pack":
                StreamResourceInfo svgStreamInfo = null;
                if (svgSource.ToString().IndexOf("siteoforigin", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    svgStreamInfo = Application.GetRemoteStream(svgSource);
                }
                else
                {
                    svgStreamInfo = Application.GetResourceStream(svgSource);
                }

                Stream svgStream = (svgStreamInfo != null) ? svgStreamInfo.Stream : null;

                if (svgStream != null)
                {
                    string fileExt      = Path.GetExtension(svgSource.ToString());
                    bool   isCompressed = !string.IsNullOrWhiteSpace(fileExt) &&
                                          string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase);

                    if (isCompressed)
                    {
                        using (svgStream)
                        {
                            using (var zipStream = new GZipStream(svgStream, CompressionMode.Decompress))
                            {
                                using (FileSvgReader reader = new FileSvgReader(settings))
                                {
                                    DrawingGroup drawGroup = reader.Read(zipStream);

                                    if (drawGroup != null)
                                    {
                                        return(drawGroup);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        using (svgStream)
                        {
                            using (FileSvgReader reader = new FileSvgReader(settings))
                            {
                                DrawingGroup drawGroup = reader.Read(svgStream);

                                if (drawGroup != null)
                                {
                                    return(drawGroup);
                                }
                            }
                        }
                    }
                }
                break;

            case "data":
                var sourceData = svgSource.OriginalString.Replace(" ", "");

                int nColon     = sourceData.IndexOf(":", StringComparison.OrdinalIgnoreCase);
                int nSemiColon = sourceData.IndexOf(";", StringComparison.OrdinalIgnoreCase);
                int nComma     = sourceData.IndexOf(",", StringComparison.OrdinalIgnoreCase);

                string sMimeType = sourceData.Substring(nColon + 1, nSemiColon - nColon - 1);
                string sEncoding = sourceData.Substring(nSemiColon + 1, nComma - nSemiColon - 1);

                if (string.Equals(sMimeType.Trim(), "image/svg+xml", StringComparison.OrdinalIgnoreCase) &&
                    string.Equals(sEncoding.Trim(), "base64", StringComparison.OrdinalIgnoreCase))
                {
                    string sContent   = SvgObject.RemoveWhitespace(sourceData.Substring(nComma + 1));
                    byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(),
                                                                    0, sContent.Length);
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        using (var reader = new FileSvgReader(settings))
                        {
                            DrawingGroup drawGroup = reader.Read(stream);
                            if (drawGroup != null)
                            {
                                return(drawGroup);
                            }
                        }
                    }
                }
                break;
            }

            return(null);
        }
Esempio n. 5
0
        /// <summary>
        /// Performs the conversion of a valid SVG source file to the <see cref="DrawingGroup"/>.
        /// </summary>
        /// <param name="svgSource">A <see cref="Uri"/> defining the path to the SVG source.</param>
        /// <param name="settings">
        /// This specifies the settings used by the rendering or drawing engine.
        /// If this is <see langword="null"/>, the default settings is used.
        /// </param>
        /// <returns>
        /// This returns <see cref="DrawingGroup"/> if successful; otherwise, it
        /// returns <see langword="null"/>.
        /// </returns>
        protected virtual DrawingGroup CreateDrawing(Uri svgSource, WpfDrawingSettings settings)
        {
            if (svgSource == null)
            {
                return(null);
            }

            string scheme = svgSource.Scheme;

            if (string.IsNullOrWhiteSpace(scheme))
            {
                return(null);
            }

            var comparer = StringComparison.OrdinalIgnoreCase;

            DrawingGroup drawing = null;

            switch (scheme)
            {
            case "file":
            //case "ftp":
            case "https":
            case "http":
                using (FileSvgReader reader = new FileSvgReader(settings))
                {
                    drawing = reader.Read(svgSource);
                }
                break;

            case "pack":
                StreamResourceInfo svgStreamInfo = null;
                if (svgSource.ToString().IndexOf("siteoforigin", comparer) >= 0)
                {
                    svgStreamInfo = Application.GetRemoteStream(svgSource);
                }
                else
                {
                    svgStreamInfo = Application.GetResourceStream(svgSource);
                }

                Stream svgStream = (svgStreamInfo != null) ? svgStreamInfo.Stream : null;

                if (svgStream != null)
                {
                    string fileExt      = Path.GetExtension(svgSource.ToString());
                    bool   isCompressed = !string.IsNullOrWhiteSpace(fileExt) &&
                                          string.Equals(fileExt, ".svgz", comparer);

                    if (isCompressed)
                    {
                        using (svgStream)
                        {
                            using (GZipStream zipStream = new GZipStream(svgStream, CompressionMode.Decompress))
                            {
                                using (FileSvgReader reader = new FileSvgReader(settings))
                                {
                                    drawing = reader.Read(zipStream);
                                }
                            }
                        }
                    }
                    else
                    {
                        using (svgStream)
                        {
                            using (FileSvgReader reader = new FileSvgReader(settings))
                            {
                                drawing = reader.Read(svgStream);
                            }
                        }
                    }
                }
                break;

            case "data":
                var sourceData = svgSource.OriginalString.Replace(" ", "");

                int nColon     = sourceData.IndexOf(":", comparer);
                int nSemiColon = sourceData.IndexOf(";", comparer);
                int nComma     = sourceData.IndexOf(",", comparer);

                string sMimeType = sourceData.Substring(nColon + 1, nSemiColon - nColon - 1);
                string sEncoding = sourceData.Substring(nSemiColon + 1, nComma - nSemiColon - 1);

                if (string.Equals(sMimeType.Trim(), "image/svg+xml", comparer) &&
                    string.Equals(sEncoding.Trim(), "base64", comparer))
                {
                    string sContent   = SvgObject.RemoveWhitespace(sourceData.Substring(nComma + 1));
                    byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(),
                                                                    0, sContent.Length);
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        using (var reader = new FileSvgReader(settings))
                        {
                            drawing = reader.Read(stream);
                        }
                    }
                }
                break;
            }

            return(drawing);
        }
Esempio n. 6
0
        private ImageSource GetImage(SvgImageElement element, WpfDrawingContext context)
        {
            bool   isSavingImages = _saveImages;
            string imagesDir      = _saveDirectory;

            if (context != null && context.Settings.IncludeRuntime == false)
            {
                isSavingImages = true;
                if (string.IsNullOrWhiteSpace(_saveDirectory) ||
                    Directory.Exists(_saveDirectory) == false)
                {
                    var assembly = Assembly.GetExecutingAssembly();
                    if (assembly != null)
                    {
                        imagesDir = PathUtils.Combine(assembly);
                    }
                }
            }

            var comparer = StringComparison.OrdinalIgnoreCase;

            string sURI       = element.Href.AnimVal.Replace(" ", string.Empty);
            int    nColon     = sURI.IndexOf(":", comparer);
            int    nSemiColon = sURI.IndexOf(";", comparer);
            int    nComma     = sURI.IndexOf(",", comparer);

            string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1);

            var  sContent   = SvgObject.RemoveWhitespace(sURI.Substring(nComma + 1));
            var  imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(), 0, sContent.Length);
            bool isGZiped   = sContent.StartsWith(SvgConstants.GZipSignature, StringComparison.Ordinal);
            bool isSvgOrXml = sContent.StartsWith(SvgConstants.SvgSignature, StringComparison.Ordinal) ||
                              sContent.StartsWith(SvgConstants.XmlSignature, StringComparison.Ordinal);

            if (string.Equals(sMimeType, "image/svg+xml", comparer) || isSvgOrXml)
            {
                if (isGZiped)
                {
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        using (GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress))
                        {
                            using (var reader = new FileSvgReader(context.Settings, true))
                            {
                                return(new DrawingImage(reader.Read(zipStream)));
                            }
                        }
                    }
                }
                else
                {
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        using (var reader = new FileSvgReader(context.Settings, true))
                        {
                            return(new DrawingImage(reader.Read(stream)));
                        }
                    }
                }
            }

            var memStream = new MemoryStream(imageBytes);

            BitmapImage imageSource = new BitmapImage();

            imageSource.BeginInit();
            imageSource.StreamSource  = memStream;
            imageSource.CacheOption   = BitmapCacheOption.OnLoad;
            imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
            imageSource.EndInit();

            string imagePath = null;

            if (isSavingImages && !string.IsNullOrWhiteSpace(imagesDir) && Directory.Exists(imagesDir))
            {
                imagePath = this.GetImagePath(imagesDir);

                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(imageSource));

                using (var fileStream = new FileStream(imagePath, FileMode.Create))
                {
                    encoder.Save(fileStream);
                }

                imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreImageCache;
                imageSource.UriSource     = new Uri(imagePath);

                //imageSource.StreamSource.Dispose();
                //imageSource = null;

                //BitmapImage savedSource = new BitmapImage();

                //savedSource.BeginInit();
                //savedSource.CacheOption   = BitmapCacheOption.None;
                //savedSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreImageCache;
                //savedSource.UriSource = new Uri(imagePath);
                //savedSource.EndInit();

                //savedSource.Freeze();

                //if (_imageCreated != null)
                //{
                //    var eventArgs = new EmbeddedImageSerializerArgs(imagePath, savedSource);
                //    _imageCreated.Invoke(this, eventArgs);
                //}

                //return savedSource;
            }
            else if (_converterFallback)
            {
                //if (_imageCreated != null)
                //{
                //    var eventArgs = new EmbeddedImageSerializerArgs(imagePath, imageSource);
                //    _imageCreated.Invoke(this, eventArgs);
                //}
                return(new EmbeddedBitmapSource(memStream, imageSource));
            }
            if (_imageCreated != null)
            {
                var eventArgs = new EmbeddedImageSerializerArgs(imagePath, imageSource);
                _imageCreated.Invoke(this, eventArgs);
            }

            imageSource.Freeze();

            return(imageSource);
        }