Inheritance: SvgTransformableElement, ISvgImageElement
        public override BitmapSource Visit(SvgImageElement element, 
            WpfDrawingContext context)
        {
            string sURI    = element.Href.AnimVal;
            int nColon     = sURI.IndexOf(":");
            int nSemiColon = sURI.IndexOf(";");
            int nComma     = sURI.IndexOf(",");

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

            string sContent   = sURI.Substring(nComma + 1);
            byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(),
                0, sContent.Length);

            //BitmapImage imageSource = new BitmapImage();
            //imageSource.BeginInit();
            //imageSource.StreamSource = new MemoryStream(imageBytes);
            //imageSource.EndInit();

            return new EmbeddedBitmapSource(new MemoryStream(imageBytes));
        }
Example #2
0
        private Image GetBitmap(SvgImageElement element)
        {
            if (!element.IsSvgImage)
            {
                if (!element.Href.AnimVal.StartsWith("data:"))
                {
                    SvgUriReference svgUri = element.UriReference;
                    Uri imageUri = new Uri(svgUri.AbsoluteUri);
                    if (imageUri.IsFile && File.Exists(imageUri.LocalPath))
                    {
                        return Bitmap.FromFile(imageUri.LocalPath);
                    }

                    WebResponse resource = svgUri.ReferencedResource;
                    if (resource == null)
                    {
                        return null;
                    }

                    Stream stream = resource.GetResponseStream();
                    if (stream == null)
                    {
                        return null;
                    }

                    return Bitmap.FromStream(stream);
                }
                else
                {
                    string sURI    = element.Href.AnimVal;
                    int nColon     = sURI.IndexOf(":");
                    int nSemiColon = sURI.IndexOf(";");
                    int nComma     = sURI.IndexOf(",");

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

                    string sContent  = sURI.Substring(nComma + 1);
                    byte[] bResult   = Convert.FromBase64CharArray(sContent.ToCharArray(),
                        0, sContent.Length);

                    MemoryStream ms = new MemoryStream(bResult);

                    return Bitmap.FromStream(ms);
                }
            }
            else
            {
                return null;
            }
        }
 public abstract BitmapSource Visit(SvgImageElement element, 
     WpfDrawingContext context);
Example #4
0
        private ImageSource GetBitmapSource(SvgImageElement element, WpfDrawingContext context)
        {
            BitmapSource bitmapSource = this.GetBitmap(element, context);
            if (bitmapSource == null)
            {
                return bitmapSource;
            }

            SvgColorProfileElement colorProfile = (SvgColorProfileElement)element.ColorProfile;
            if (colorProfile == null)
            {
                return bitmapSource;
            }
            else
            {
                BitmapFrame bitmapSourceFrame   = BitmapFrame.Create(bitmapSource);
                ColorContext sourceColorContext = null;
                IList<ColorContext> colorContexts = bitmapSourceFrame.ColorContexts;
                if (colorContexts != null && colorContexts.Count != 0)
                {
                    sourceColorContext = colorContexts[0];
                }
                else
                {
                    sourceColorContext = new ColorContext(bitmapSource.Format);
                    //sourceColorContext = new ColorContext(PixelFormats.Default);
                }

                SvgUriReference svgUri = colorProfile.UriReference;
                Uri profileUri = new Uri(svgUri.AbsoluteUri);

                ColorContext destColorContext = new ColorContext(profileUri);
                ColorConvertedBitmap convertedBitmap = new ColorConvertedBitmap(bitmapSource,
                    sourceColorContext, destColorContext, bitmapSource.Format);

                return convertedBitmap;
            }
        }
Example #5
0
        private BitmapSource GetBitmap(SvgImageElement element, WpfDrawingContext context)
        {
            if (element.IsSvgImage)
            {
                return null;
            }

            if (!element.Href.AnimVal.StartsWith("data:"))
            {
                SvgUriReference svgUri = element.UriReference;
                string absoluteUri = svgUri.AbsoluteUri;
                if (String.IsNullOrEmpty(absoluteUri))
                {
                    return null; // most likely, the image does not exist...
                }

                Uri imageUri = new Uri(svgUri.AbsoluteUri);
                if (imageUri.IsFile)
                {
                    if (File.Exists(imageUri.LocalPath))
                    {
                        BitmapImage imageSource = new BitmapImage();

                        imageSource.BeginInit();
                        imageSource.CacheOption = BitmapCacheOption.OnLoad;
                        imageSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache
                            | BitmapCreateOptions.PreservePixelFormat;
                        imageSource.UriSource     = imageUri;
                        imageSource.EndInit();

                        return imageSource;
                    }

                    return null;
                }
                else
                {
                    Stream stream = svgUri.ReferencedResource.GetResponseStream();

                    BitmapImage imageSource = new BitmapImage();
                    imageSource.BeginInit();
                    imageSource.CacheOption   = BitmapCacheOption.OnLoad;
                    imageSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache
                        | BitmapCreateOptions.PreservePixelFormat;
                    imageSource.StreamSource = stream;
                    imageSource.EndInit();

                    return imageSource;
                }
            }
            else
            {
                WpfEmbeddedImageVisitor imageVisitor = context.ImageVisitor;
                if (imageVisitor != null)
                {
                    BitmapSource visitorSource = imageVisitor.Visit(element, context);
                    if (visitorSource != null)
                    {
                        return visitorSource;
                    }
                }

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

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

                string sContent  = sURI.Substring(nComma + 1);
                byte[] bResult   = Convert.FromBase64CharArray(sContent.ToCharArray(),
                    0, sContent.Length);

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

                return imageSource;
            }
        }