Ejemplo n.º 1
0
        internal virtual CanvasBitmap BitmapForId(CanvasDevice device, string id)
        {
            lock (this)
            {
                if (!_bitmaps.TryGetValue(id, out CanvasBitmap bitmap))
                {
                    var imageAsset = _imageAssets[id];
                    if (imageAsset == null)
                    {
                        return(null);
                    }
                    if (_delegate != null)
                    {
                        bitmap = _delegate.FetchBitmap(imageAsset);
                        if (bitmap != null)
                        {
                            _bitmaps[id] = bitmap;
                        }
                        return(bitmap);
                    }

                    Stream @is;
                    try
                    {
                        if (string.IsNullOrEmpty(_imagesFolder))
                        {
                            throw new InvalidOperationException("You must set an images folder before loading an image." + " Set it with LottieDrawable.ImageAssetsFolder");
                        }
                        @is = File.OpenRead(_imagesFolder + imageAsset.FileName);
                    }
                    catch (IOException e)
                    {
                        Debug.WriteLine($"Unable to open asset. {e}", LottieLog.Tag);
                        return(null);
                    }
                    var task = CanvasBitmap.LoadAsync(device, @is.AsRandomAccessStream(), 160).AsTask();
                    task.Wait();
                    bitmap = task.Result;

                    @is.Dispose();

                    _bitmaps[id] = bitmap;
                }
                return(bitmap);
            }
        }
Ejemplo n.º 2
0
        internal virtual Bitmap BitmapForId(RenderTarget renderTarget, string id)
        {
            lock (this)
            {
                if (!_imageAssets.TryGetValue(id, out var imageAsset))
                {
                    return(null);
                }
                else if (imageAsset.Bitmap != null)
                {
                    return(imageAsset.Bitmap);
                }

                Bitmap bitmap;

                if (_delegate != null)
                {
                    bitmap = _delegate.FetchBitmap(imageAsset);
                    if (bitmap != null)
                    {
                        PutBitmap(id, bitmap);
                    }
                    return(bitmap);
                }

                var filename = imageAsset.FileName;

                if (filename.StartsWith("data:") && filename.IndexOf("base64,") > 0)
                {
                    // Contents look like a base64 data URI, with the format data:image/png;base64,<data>.
                    byte[] data;
                    try
                    {
                        data = Convert.FromBase64String(filename.Substring(filename.IndexOf(',') + 1));
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine($"data URL did not have corRectangleF base64 format. {e}", LottieLog.Tag);
                        return(null);
                    }

                    //TODO: OID: Check this way is ok or not
                    bitmap = new Bitmap(renderTarget, new SharpDX.Size2(0, 0));
                    bitmap.CopyFromMemory(data, 160);

                    PutBitmap(id, bitmap);
                    return(bitmap);
                }

                Stream @is;
                try
                {
                    if (string.IsNullOrEmpty(_imagesFolder))
                    {
                        throw new InvalidOperationException("You must set an images folder before loading an image. Set it with LottieDrawable.ImageAssetsFolder");
                    }
                    @is = File.OpenRead(_imagesFolder + imageAsset.FileName);
                }
                catch (IOException e)
                {
                    Debug.WriteLine($"Unable to open asset. {e}", LottieLog.Tag);
                    return(null);
                }

                //TODO: OID: Check this way is ok or not
                bitmap = new Bitmap(renderTarget, new SharpDX.Size2(0, 0));
                bitmap.CopyFromStream(@is, 160, (int)@is.Length);

                @is.Dispose();

                PutBitmap(id, bitmap);

                return(bitmap);
            }
        }
Ejemplo n.º 3
0
        internal virtual Bitmap BitmapForId(  string id)
        {
            lock (this)
            {
                if (!_imageAssets.TryGetValue(id, out var imageAsset))
                {
                    return(null);
                }
                else if (imageAsset.Bitmap != null)
                {
                    return(imageAsset.Bitmap);
                }

                Bitmap bitmap;

                if (_delegate != null)
                {
                    bitmap = _delegate.FetchBitmap(imageAsset);
                    if (bitmap != null)
                    {
                        PutBitmap(id, bitmap);
                    }
                    return(bitmap);
                }

                var filename = imageAsset.FileName;

                if (filename.StartsWith("data:") && filename.IndexOf("base64,") > 0)
                {
                    // Contents look like a base64 data URI, with the format data:image/png;base64,<data>.
                    byte[] data;
                    try
                    {
                        data = Convert.FromBase64String(filename.Substring(filename.IndexOf(',') + 1));
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine($"data URL did not have corRect base64 format. {e}", LottieLog.Tag);
                        return(null);
                    }

                    bitmap = LoadFromBuffer( data);

                    PutBitmap(id, bitmap);
                    return(bitmap);
                }

                Stream @is;
                try
                {
                    if (string.IsNullOrEmpty(_imagesFolder))
                    {
                        throw new InvalidOperationException("You must set an images folder before loading an image. Set it with LottieDrawable.ImageAssetsFolder");
                    }
                    @is = File.OpenRead(_imagesFolder + imageAsset.FileName);
                }
                catch (IOException e)
                {
                    Debug.WriteLine($"Unable to open asset. {e}", LottieLog.Tag);
                    return(null);
                }

                bitmap = LoadFromStream( @is);

                @is.Dispose();

                PutBitmap(id, bitmap);

                return(bitmap);
            }
        }
Ejemplo n.º 4
0
        internal CanvasBitmap BitmapForId(CanvasDevice device, string id)
        {
            lock (this)
            {
                if (!_imageAssets.TryGetValue(id, out var asset))
                {
                    return(null);
                }
                var bitmap = asset.Bitmap;
                if (bitmap != null)
                {
                    return(bitmap);
                }

                if (_delegate != null)
                {
                    bitmap = _delegate.FetchBitmap(asset);
                    if (bitmap != null)
                    {
                        PutBitmap(id, bitmap);
                    }
                    return(bitmap);
                }

                var filename             = asset.FileName;
                Task <CanvasBitmap> task = null;
                Stream @is;

                if (filename.StartsWith("data:") && filename.IndexOf("base64,") > 0)
                {
                    // Contents look like a base64 data URI, with the format data:image/png;base64,<data>.
                    byte[] data;
                    try
                    {
                        data = Convert.FromBase64String(filename.Substring(filename.IndexOf(',') + 1));
                        @is  = new MemoryStream(data);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine($"data URL did not have correct base64 format. {e}", LottieLog.Tag);
                        return(null);
                    }
                    task = CanvasBitmap.LoadAsync(device, @is.AsRandomAccessStream(), 160).AsTask();
                    task.Wait();
                    bitmap = task.Result;

                    @is.Dispose();

                    PutBitmap(id, bitmap);
                    return(bitmap);
                }

                try
                {
                    if (string.IsNullOrEmpty(_imagesFolder))
                    {
                        throw new InvalidOperationException("You must set an images folder before loading an image. Set it with LottieDrawable.ImageAssetsFolder");
                    }
                    @is = File.OpenRead(_imagesFolder + asset.FileName);
                }
                catch (IOException e)
                {
                    Debug.WriteLine($"Unable to open asset. {e}", LottieLog.Tag);
                    return(null);
                }
                task = CanvasBitmap.LoadAsync(device, @is.AsRandomAccessStream(), 160).AsTask();
                task.Wait();
                bitmap = task.Result;

                @is.Dispose();

                PutBitmap(id, bitmap);

                return(bitmap);
            }
        }