コード例 #1
0
        /// <inheritdoc />
        public IObservable <ImageHandle> LoadImage(string path, IScheduler executionScheduler = null)
        {
            Guard.Against.ArgumentNullOrEmpty(path, nameof(path));
            executionScheduler ??= RxApp.TaskpoolScheduler;

            return(Observable.Create <ImageHandle>(observer =>
            {
                CompositeDisposable disposable = new CompositeDisposable();
                ImageHandle handle = default;
                SKCodec codec = default;

                IDisposable scheduling = executionScheduler.Schedule(() =>
                {
                    try
                    {
                        codec = SKCodec.Create(path) ?? throw new ImageLoadingException(path);
                        codec.DisposeWith(disposable);

                        var bitmap = SKBitmap.Decode(codec);
                        var format = codec.EncodedFormat == SKEncodedImageFormat.Gif ? SKEncodedImageFormat.Png : codec.EncodedFormat;
                        handle = new ImageHandle(path, bitmap, format);
                        observer.OnNext(handle);
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                    }
                    observer.OnCompleted();
                }).DisposeWith(disposable);

                return disposable;
            }));
        }