Example #1
0
        public async Task <IDecodedImage <Bitmap> > DecodeAsync(Stream stream, string path, ImageSource source, ImageInformation imageInformation, TaskParameter parameters)
        {
            var result = new DecodedImage <Bitmap>();

            using (var gifDecoder = new GifHelper())
            {
                var insampleSize = 1;

                // DOWNSAMPLE
                if (parameters.DownSampleSize != null && (parameters.DownSampleSize.Item1 > 0 || parameters.DownSampleSize.Item2 > 0))
                {
                    // Calculate inSampleSize
                    var downsampleWidth  = parameters.DownSampleSize.Item1;
                    var downsampleHeight = parameters.DownSampleSize.Item2;

                    if (parameters.DownSampleUseDipUnits)
                    {
                        downsampleWidth  = downsampleWidth.DpToPixels();
                        downsampleHeight = downsampleHeight.DpToPixels();
                    }
                    await gifDecoder.ReadHeaderAsync(stream).ConfigureAwait(false);

                    insampleSize = BaseDecoder.CalculateInSampleSize(gifDecoder.Width, gifDecoder.Height, downsampleWidth, downsampleHeight, false);
                }

                await gifDecoder.ReadAsync(stream, insampleSize).ConfigureAwait(false);

                gifDecoder.Advance();

                imageInformation.SetOriginalSize(gifDecoder.Width, gifDecoder.Height);

                if (insampleSize > 1)
                {
                    imageInformation.SetCurrentSize(gifDecoder.DownsampledWidth, gifDecoder.DownsampledHeight);
                }
                else
                {
                    imageInformation.SetCurrentSize(gifDecoder.Width, gifDecoder.Height);
                }

                result.IsAnimated = gifDecoder.FrameCount > 1 && Configuration.AnimateGifs;

                if (result.IsAnimated && Configuration.AnimateGifs)
                {
                    result.AnimatedImages = new AnimatedImage <Bitmap> [gifDecoder.FrameCount];

                    for (var i = 0; i < gifDecoder.FrameCount; i++)
                    {
                        var animatedImage = new AnimatedImage <Bitmap>
                        {
                            Delay = gifDecoder.GetDelay(i),
                            Image = await gifDecoder.GetNextFrameAsync()
                        };
                        result.AnimatedImages[i] = animatedImage;

                        gifDecoder.Advance();
                    }
                }
                else
                {
                    result.IsAnimated = false;
                    result.Image      = await gifDecoder.GetNextFrameAsync();
                }



                if (result.Image != null)
                {
                    imageInformation.SetOriginalSize(result.Image.Width, result.Image.Height);
                    imageInformation.SetCurrentSize(result.Image.Width, result.Image.Height);
                }
                else if (result.AnimatedImages != null)
                {
                    if (result.AnimatedImages.Length > 0)
                    {
                        if (result.AnimatedImages[0].Image != null)
                        {
                            imageInformation.SetOriginalSize(result.AnimatedImages[0].Image.Width, result.AnimatedImages[0].Image.Height);
                            imageInformation.SetCurrentSize(result.AnimatedImages[0].Image.Width, result.AnimatedImages[0].Image.Height);
                        }
                    }
                }

                return(result);
            }
        }