Example #1
0
        /// <summary>
        /// Asynchronously returns a byte array containing the thumbnail image.
        /// </summary>
        /// <param name="uri">The image uri.</param>
        /// <returns>A byte array containing the thumbnail image.</returns>
        public static async Task <byte[]> GetThumbnailAsync(Uri uri)
        {
            try
            {
                var file = await StorageFile.GetFileFromApplicationUriAsync(uri)
                           .AsTask()
                           .ConfigureAwait(false);

                using (var fileStream = await file.OpenReadAsync().AsTask().ConfigureAwait(false))
                {
                    BitmapDecoder decoder = await BitmapDecoder
                                            .CreateAsync(fileStream)
                                            .AsTask()
                                            .ConfigureAwait(false);

                    using (var imageStream = await decoder.GetThumbnailAsync().AsTask().ConfigureAwait(false))
                        using (var readStream = imageStream.AsStream())
                        {
                            byte[] thumbnail = new byte[readStream.Length];
                            await readStream.ReadAsync(thumbnail, 0, thumbnail.Length)
                            .ConfigureAwait(false);

                            return(thumbnail);
                        }
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #2
0
        public static async Task <BitmapImage> GetThumbnail(StorageFile file)
        {
            BitmapImage bi = null;

            using (var sourceStream = await file.OpenAsync(FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream);

                var stream = await decoder.GetThumbnailAsync();

                bi = new BitmapImage();
                bi.SetSource(stream);
            }
            return(bi);
        }
Example #3
0
        /// <summary>
        /// Asynchronously returns a byte array containing the thumbnail image.
        /// </summary>
        /// <param name="stream">The image stream.</param>
        /// <returns>A byte array containing the thumbnail image.</returns>
        public static async Task <byte[]> GetThumbnailAsync(IRandomAccessStream stream)
        {
            try
            {
                BitmapDecoder decoder = await BitmapDecoder
                                        .CreateAsync(stream)
                                        .AsTask()
                                        .ConfigureAwait(false);

                using (var imageStream = await decoder.GetThumbnailAsync().AsTask().ConfigureAwait(false))
                    using (var readStream = imageStream.AsStream())
                    {
                        byte[] thumbnail = new byte[readStream.Length];
                        await readStream.ReadAsync(thumbnail, 0, thumbnail.Length)
                        .ConfigureAwait(false);

                        return(thumbnail);
                    }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #4
0
        /// <summary>
        /// Create a thumbnail image from the input. The thumbnail is taken from the image file in the case that the file has one.
        /// Otherwise it will be a new image that is created from the image file.
        /// </summary>
        /// <param name="decoder">Bitmap decoder.</param>
        /// <param name="expectedWidth"> Expected width.</param>
        /// <param name="expectedHeight">Expected height.</param>
        /// <returns>a new image with smallest size that is larger than the input dimension.</returns>
        public static async Task <SoftwareBitmap> CreateThumbnail(BitmapDecoder decoder, uint expectedWidth, uint expectedHeight)
        {
            BitmapDecoder decoder2;

            try
            {
                var thumbnail = await decoder.GetThumbnailAsync();

                decoder2 = await BitmapDecoder.CreateAsync(thumbnail);
            }
            catch
            {
                decoder2 = null;
            }

            if (decoder2 == null)
            {
                try
                {
                    var preview = await decoder.GetPreviewAsync();

                    decoder2 = await BitmapDecoder.CreateAsync(preview);
                }
                catch
                {
                    decoder2 = null;
                }
            }

            if (decoder2 == null)
            {
                decoder2 = decoder;
            }

            return(await CreateResizedBitmap(decoder2, expectedWidth, expectedHeight, BitmapInterpolationMode.Linear));
        }