Ejemplo n.º 1
0
        /// <summary>
        /// Loads image from data URI
        /// </summary>
        /// <param name="uri">data URI</param>
        /// <returns>loaded SVG image object, or null when loading was not successful</returns>
        private static SKSvg LoadImageFromDataUri(Uri uri)
        {
            string text = uri.OriginalString;

            string?svgText = null;

            if (text.StartsWith(SvgConstants.DataUriPlainPrefix, StringComparison.InvariantCultureIgnoreCase))
            {
                svgText = text.Substring(SvgConstants.DataUriPlainPrefix.Length);
            }
            else if (text.StartsWith(SvgConstants.DataUriBase64Prefix, StringComparison.InvariantCultureIgnoreCase))
            {
                svgText = text.Substring(SvgConstants.DataUriBase64Prefix.Length);

                var base64EncodedBytes = Convert.FromBase64String(svgText);
                svgText = Encoding.UTF8.GetString(base64EncodedBytes);
            }

            if (svgText == null)
            {
                throw new FormatException("Invalid SVG image text format: " + uri.OriginalString);
            }

            var svg = new SKSvg();

            svg.FromSvg(svgText);

            return(svg);
        }
Ejemplo n.º 2
0
        public static SKSvg?LoadSvg(this string?str)
        {
            if (str == null)
            {
                return(null);
            }

            var svg = new SKSvg();

            svg.FromSvg(str);
            return(svg);
        }
Ejemplo n.º 3
0
        public static BitmapInfo LoadBitmap(object bitmapStream)
        {
            // todo: Our BitmapRegistry stores not only bitmaps. Perhaps we should store a class in it
            // which has all information. So we should have a SymbolImageRegistry in which we store a
            // SymbolImage. Which holds the type, data and other parameters.

            if (bitmapStream is string str)
            {
                if (str.ToLower().Contains("<svg"))
                {
                    var svg = new SKSvg();
                    svg.FromSvg(str);

                    return(new BitmapInfo {
                        Svg = svg
                    });
                }
            }

            if (bitmapStream is Stream stream)
            {
                if (stream.IsSvg())
                {
                    var svg = new SKSvg();
                    svg.Load(stream);

                    return(new BitmapInfo {
                        Svg = svg
                    });
                }

                var image = SKImage.FromEncodedData(SKData.CreateCopy(stream.ToBytes()));
                return(new BitmapInfo {
                    Bitmap = image
                });
            }

            if (bitmapStream is Sprite sprite)
            {
                return(new BitmapInfo {
                    Sprite = sprite
                });
            }

            return(null);
        }