コード例 #1
0
 public bool TryGetImage
     (string path, ImageQuality quality, out ImageSourceContainer image)
 {
     if (this.TryGetImageData(path, quality, out var result))
     {
         if (result != null)
         {
             count++;
             image = result.GetImage(count);
             return(true);
         }
     }
     image = null;
     return(false);
 }
コード例 #2
0
 public ImageBufferItem(ImageSourceContainer source, ulong initialCount)
 {
     this.image           = source;
     this.LastLoadedCount = initialCount;
 }
コード例 #3
0
        /// <summary>
        /// 画像読み込みメイン処理
        /// 同じスケジューラの上でシーケンシャルに動作
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        private async Task LoadImageAsync(CommandPacket command)
        {
            var key = command.Path;

            if (this.TryGetImage(key, command.Option.Quality, out var result))
            {
                if (result != null)
                {
                    command.Observer.OnNext(result);
                    command.Observer.OnCompleted();
                    return;
                }
            }

            var token = command.CancellationToken;

            if (token.IsCancellationRequested)
            {
                command.Observer.OnCompleted();
                return;
            }


            var image = new ImageSourceContainer();

            bool failedFlag = false;

            var option = command.Option;
            var file   = command.File;

            Size?frameSize = null;

            switch (option.Quality)
            {
            case ImageQuality.ThumbNail:
            case ImageQuality.Resized:
                //リサイズ
                if (option.FrameWidth > 1 && option.FrameHeight > 1)
                {
                    frameSize = new Size(option.FrameWidth, option.FrameHeight);
                }
                else if (option.Quality == ImageQuality.ThumbNail)
                {
                    frameSize = this.lowQualitySize;
                }
                break;

            case ImageQuality.LowQuality:
                //低画質読み込み
                frameSize = this.lowQualitySize;
                break;

            case ImageQuality.OriginalSize:
                //オリジナルサイズで読み込み
                frameSize = null;
                break;

            default:
                break;
            }

            var asThumbnail = option.Quality <= ImageQuality.LowQuality;

            try
            {
                if (file != null)
                {
                    await image.LoadImageAsync
                        (file, frameSize, asThumbnail, option.IsFill, option.CmsEnable);
                }
                else
                {
                    await image.LoadImageAsync
                        (key, frameSize, asThumbnail, option.IsFill, option.CmsEnable);
                }
            }
            catch (OutOfMemoryException e)
            {
                if (option.Quality == ImageQuality.ThumbNail)
                {
                    ClearBuffer();
                    command.Observer.OnError(e);
                    return;
                }
                else
                {
                    failedFlag = true;
                }
            }
            catch (Exception e)
            {
                command.Observer.OnError(e);
                return;
            }


            if (failedFlag)
            {
                if (token.IsCancellationRequested)
                {
                    command.Observer.OnCompleted();
                    return;
                }


                this.ClearBuffer();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                await Task.Delay(300);

                image = new ImageSourceContainer();

                if (!frameSize.HasValue ||
                    frameSize.Value.Width > failedLoadingLength ||
                    frameSize.Value.Height > failedLoadingLength)
                {
                    //サイズ小さめ
                    frameSize = failedLoadingSize;
                }

                var reloadOption = command.Option.Clone();

                try
                {
                    if (file != null)
                    {
                        await image.LoadImageAsync
                            (file, frameSize, asThumbnail, option.IsFill, option.CmsEnable);
                    }
                    else
                    {
                        await image.LoadImageAsync
                            (key, frameSize, asThumbnail, option.IsFill, option.CmsEnable);
                    }
                }
                catch (Exception e)
                {
                    command.Observer.OnError(e);
                    return;
                }
            }

            if (image.HasImage())
            {
                if (option.Quality == ImageQuality.ThumbNail || image.Quality <= ImageQuality.LowQuality)
                {
                    thumbNailImages.AddOrExtrude
                        (key, new ImageBufferItem(image, ++this.count), thumbNailbufferSize);
                }
                else
                {
                    images.AddOrExtrude
                        (key, new ImageBufferItem(image, ++this.count), bufferSize);
                }

                this.UpdatedSubject.OnNext(key);
                command.Observer.OnNext(image);
            }
            command.Observer.OnCompleted();
        }