Example #1
0
        public static async Task <Bitmap> GetBitmapAsync(SvgImage svgImage, int width, int height)
        {
            Bitmap result = null;

            Stream svgStream = await SvgService.GetSvgStreamAsync(svgImage).ConfigureAwait(false);

            await Task.Run(() =>
            {
                var svgReader = new SvgReader(new StreamReader(svgStream));

                var graphics = svgReader.Graphic;

                var scale = 1.0;

                if (height >= width)
                {
                    scale = height / graphics.Size.Height;
                }
                else
                {
                    scale = width / graphics.Size.Width;
                }

                var canvas = new AndroidPlatform().CreateImageCanvas(graphics.Size, scale);
                graphics.Draw(canvas);
                var image = (BitmapImage)canvas.GetImage();
                result    = image.Bitmap;
            });

            return(result);
        }
Example #2
0
        public async void Render( )
        {
            //added check because both *may* not be set at the same time, this negates the error throwing unfortuantely. --bwc
            if (String.IsNullOrEmpty(Element.SvgPath) || (Element.SvgAssembly == null))
            {
                return;
            }

            if (_formsControl != null)
            {
                await Task.Run(async( ) =>
                {
                    Stream svgStream = _formsControl.SvgAssembly.GetManifestResourceStream(_formsControl.SvgPath);

                    if (svgStream == null)
                    {
                        throw new Exception(string.Format("Error retrieving {0} make sure Build Action is Embedded Resource", _formsControl.SvgPath));
                    }

                    SvgReader r = new SvgReader(new StreamReader(svgStream), new StylesParser(new ValuesParser( )), new ValuesParser( ));

                    this.ReplaceColors(r.Graphic, Element.ReplacementColors);

                    Graphic graphics = r.Graphic;
                    int width        = PixelToDP((int)_formsControl.WidthRequest <= 0 ? 100 : (int)_formsControl.WidthRequest);
                    int height       = PixelToDP((int)_formsControl.HeightRequest <= 0 ? 100 : (int)_formsControl.HeightRequest);
                    double scale     = 1.0;

                    if (height >= width)
                    {
                        scale = height / graphics.Size.Height;
                    }
                    else
                    {
                        scale = width / graphics.Size.Width;
                    }

                    IImageCanvas canvas = new AndroidPlatform( ).CreateImageCanvas(graphics.Size, scale);

                    graphics.Draw(canvas);
                    BitmapImage image = (BitmapImage)canvas.GetImage( );


                    return(image);
                }).ContinueWith(taskResult =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        var imageView = new ImageView(Context);
                        imageView.SetScaleType(ImageView.ScaleType.FitXy);
                        imageView.SetImageBitmap(taskResult.Result.Bitmap);
                        SetNativeControl(imageView);
                    });
                });
            }
        }
        protected override async void OnElementChanged(ElementChangedEventArgs <SvgImage> e)
        {
            base.OnElementChanged(e);

            if (_formsControl != null)
            {
                await Task.Run(async() =>
                {
                    var svgStream = _formsControl.SvgAssembly.GetManifestResourceStream(_formsControl.SvgPath);

                    if (svgStream == null)
                    {
                        throw new Exception(string.Format("Error retrieving {0} make sure Build Action is Embedded Resource",
                                                          _formsControl.SvgPath));
                    }

                    var r = new SvgReader(new StreamReader(svgStream), new StylesParser(new ValuesParser()), new ValuesParser());

                    var graphics = r.Graphic;

                    var width  = PixelToDP((int)_formsControl.WidthRequest <= 0 ? 100 : (int)_formsControl.WidthRequest);
                    var height = PixelToDP((int)_formsControl.HeightRequest <= 0 ? 100 : (int)_formsControl.HeightRequest);

                    var scale = 1.0;

                    if (height >= width)
                    {
                        scale = height / graphics.Size.Height;
                    }
                    else
                    {
                        scale = width / graphics.Size.Width;
                    }

                    var canvas = new AndroidPlatform().CreateImageCanvas(graphics.Size, scale);
                    graphics.Draw(canvas);
                    var image = (BitmapImage)canvas.GetImage();

                    return(image);
                }).ContinueWith(taskResult =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        var imageView = new ImageView(Context);

                        imageView.SetScaleType(ImageView.ScaleType.CenterInside);
                        imageView.SetImageBitmap(taskResult.Result.Bitmap);

                        SetNativeControl(imageView);
                    });
                });
            }
        }
        async void UpdateSVGSource()
        {
            await Task.Run(async() =>
            {
                Uri u;
                try{
                    u = new Uri(_formsControl.SvgPath);
                }catch (Exception e) {
                    Insights.Report(e);
                    return(null);
                }
                var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.GetFileName(u.AbsolutePath));
                if (!File.Exists(path))
                {
                    using (var client = new HttpClient()) {
                        try{
                            var bytes = await client.GetByteArrayAsync(_formsControl.SvgPath);
                            File.WriteAllBytes(path, bytes);
                        }catch (Exception e) {
                            Insights.Report(e);
                            return(null);
                        }
                    }
                }

                var svgStream = File.OpenRead(path);

                if (svgStream == null)
                {
                    throw new Exception(string.Format("Error retrieving {0} make sure Build Action is Embedded Resource",
                                                      _formsControl.SvgPath));
                }
                SvgReader r;
                try{
                    r = new SvgReader(new StreamReader(svgStream), new StylesParser(new ValuesParser()), new ValuesParser());
                }catch (Exception e) {
                    Insights.Report(e);
                    return(null);
                }

                var graphics = r.Graphic;

                var width  = PixelToDP((int)_formsControl.WidthRequest <= 0 ? 100 : (int)_formsControl.WidthRequest);
                var height = PixelToDP((int)_formsControl.HeightRequest <= 0 ? 100 : (int)_formsControl.HeightRequest);

                var scale = 1.0;

                if (height >= width)
                {
                    scale = height / graphics.Size.Height;
                }
                else
                {
                    scale = width / graphics.Size.Width;
                }

                try{
                    var canvas = new AndroidPlatform().CreateImageCanvas(graphics.Size, scale);
                    graphics.Draw(canvas);
                    var image = (BitmapImage)canvas.GetImage();
                    return(image);
                }
                catch (Exception e) {
                    Insights.Report(e);
                    return(null);
                }
            }).ContinueWith(taskResult =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    var imageView = new ImageView(Context);
                    SetNativeControl(imageView);
                    if (taskResult.Result == null)
                    {
                        var id = getResourceId(_formsControl.SvgDefaultImage, "drawable", Context.PackageName);
                        Control.SetImageResource(id);
                        return;
                    }

                    imageView.SetScaleType(ImageView.ScaleType.CenterInside);
                    imageView.SetImageBitmap(taskResult.Result.Bitmap);
                });
            });
        }