Esempio n. 1
0
        protected object GetImage(string uriString)
        {
            try
            {
                var uri = new Uri(uriString.Substring(0, 65519)); //Uri MaxLength is 65519 (https://msdn.microsoft.com/en-us/library/z6c2z492.aspx)

                // handle data/uri embedded images (http://en.wikipedia.org/wiki/Data_URI_scheme)
                if (uri.IsAbsoluteUri && uri.Scheme == "data")
                {
                    int dataIdx = uriString.IndexOf(",") + 1;
                    if (dataIdx <= 0 || dataIdx + 1 > uriString.Length)
                    {
                        throw new Exception("Invalid data URI");
                    }

                    // we're assuming base64, as ascii encoding would be *highly* unsusual for images
                    // also assuming it's png or jpeg mimetype
                    byte[] imageBytes = Convert.FromBase64String(uriString.Substring(dataIdx));
                    using (var stream = new MemoryStream(imageBytes))
                    {
                        return(Image.FromStream(stream));
                    }
                }

                if (!uri.IsAbsoluteUri)
                {
                    uri = new Uri(OwnerDocument.BaseUri, uri);
                }

                // should work with http: and file: protocol urls
                var httpRequest = WebRequest.Create(uri);

                using (WebResponse webResponse = httpRequest.GetResponse())
                {
                    using (var stream = webResponse.GetResponseStream())
                    {
                        if (stream.CanSeek)
                        {
                            stream.Position = 0;
                        }
                        if (uri.LocalPath.EndsWith(".svg", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var doc = SvgDocument.Open <SvgDocument>(stream);
                            doc.BaseUri = uri;
                            return(doc);
                        }
                        else
                        {
                            return(Bitmap.FromStream(stream));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SvgLogger.Instance.LogError(string.Format("Error loading image: '{0}', error: {1} ", uriString, ex.Message));
                return(null);
            }
        }
Esempio n. 2
0
        protected Image GetImage(Uri uri)
        {
            try
            {
                // handle data/uri embedded images (http://en.wikipedia.org/wiki/Data_URI_scheme)
                if (uri.IsAbsoluteUri && uri.Scheme == "data")
                {
                    string uriString = uri.OriginalString;
                    int    dataIdx   = uriString.IndexOf(",") + 1;
                    if (dataIdx <= 0 || dataIdx + 1 > uriString.Length)
                    {
                        throw new Exception("Invalid data URI");
                    }

                    // we're assuming base64, as ascii encoding would be *highly* unsusual for images
                    // also assuming it's png or jpeg mimetype
                    byte[] imageBytes = Convert.FromBase64String(uriString.Substring(dataIdx));
                    Image  image      = Image.FromStream(new MemoryStream(imageBytes));
                    return(image);
                }

                if (!uri.IsAbsoluteUri)
                {
                    uri = new Uri(OwnerDocument.BaseUri, uri);
                }

                // should work with http: and file: protocol urls
                var httpRequest = WebRequest.Create(uri);

                using (WebResponse webResponse = httpRequest.GetResponse())
                {
                    MemoryStream ms = BufferToMemoryStream(webResponse.GetResponseStream());
                    if (uri.LocalPath.EndsWith(".svg", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var doc = SvgDocument.Open <SvgDocument>(ms);
                        return(doc.Draw());
                    }
                    else
                    {
                        return(Bitmap.FromStream(ms));
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error loading image: '{0}', error: {1} ", uri, ex.Message);
                return(null);
            }
        }
Esempio n. 3
0
        public virtual SvgElement GetElementById(Uri uri)
        {
            var urlString = GetUrlString(uri.ToString());

            if (!urlString.StartsWith("#"))
            {
                var index    = urlString.LastIndexOf('#');
                var fragment = urlString.Substring(index);

                uri = new Uri(urlString.Remove(index, fragment.Length), UriKind.RelativeOrAbsolute);

                if (!uri.IsAbsoluteUri && _document.BaseUri != null)
                {
                    uri = new Uri(_document.BaseUri, uri);
                }


                if (!ResolveExternalElements.AllowsResolving(uri))
                {
                    Trace.TraceWarning("Trying to resolve element by ID from '{0}', but resolving external resources of that type is disabled.", uri);
                    return(null);
                }

                if (uri.IsAbsoluteUri)
                {
                    if (uri.IsFile)
                    {
                        var doc = SvgDocument.Open <SvgDocument>(uri.LocalPath);
                        return(doc.IdManager.GetElementById(fragment));
                    }
                    else if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)
                    {
                        var httpRequest = WebRequest.Create(uri);
                        using (var webResponse = httpRequest.GetResponse())
                        {
                            var doc = SvgDocument.Open <SvgDocument>(webResponse.GetResponseStream());
                            return(doc.IdManager.GetElementById(fragment));
                        }
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
            }

            return(GetElementById(urlString));
        }
Esempio n. 4
0
        public virtual SvgElement GetElementById(Uri uri)
        {
            var urlString = GetUrlString(uri.ToString());

            if (!urlString.StartsWith("#"))
            {
                var index    = urlString.LastIndexOf('#');
                var fragment = urlString.Substring(index);

                uri = new Uri(urlString.Remove(index, fragment.Length), UriKind.RelativeOrAbsolute);

                if (!uri.IsAbsoluteUri && _document.BaseUri != null)
                {
                    uri = new Uri(_document.BaseUri, uri);
                }

                if (uri.IsAbsoluteUri)
                {
                    if (uri.IsFile)
                    {
                        var doc = SvgDocument.Open <SvgDocument>(uri.LocalPath);
                        return(doc.IdManager.GetElementById(fragment));
                    }
                    else if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)
                    {
                        var httpRequest = WebRequest.Create(uri);
                        using (var webResponse = httpRequest.GetResponse())
                        {
                            var doc = SvgDocument.Open <SvgDocument>(webResponse.GetResponseStream());
                            return(doc.IdManager.GetElementById(fragment));
                        }
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
            }

            return(GetElementById(urlString));
        }