예제 #1
0
 private async Task updateUI(MyFilesModel item)
 {
     if (!item.ImageLoaded)
     {
         item.Bitmap = await MyFilesController.GetImage(item, (int)Window.Current.Bounds.Width);
         item.ImageLoaded = true;
     }
 }
예제 #2
0
        /// <summary>
        /// Retrieves a binary image and resizes it accordingly
        /// </summary>
        /// <param name="item">MyFilesModel object</param>
        /// <returns>resized BitmapSource</returns>
        public static async Task<BitmapSource> GetImage(MyFilesModel item, int w)
        {
            BitmapImage img = new BitmapImage();

            //ensure client created
            var client = await EnsureClient();

            //get the file stream
            using (Stream stream = await client.Files.GetById(item.Id).ToFile().DownloadAsync())
            {
                using (var memStream = new MemoryStream())
                {
                    await stream.CopyToAsync(memStream);
                    memStream.Position = 0;
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memStream.AsRandomAccessStream());
                    using (InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream())
                    {
                        //first get original specs so we can scale the resize best
                        BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);
                        await enc.FlushAsync();
                        BitmapImage original = new BitmapImage();
                        original.SetSource(ras);
                        int height = original.PixelHeight;
                        int width = original.PixelWidth;

                        //rewind and take a second pass...this time resizing
                        ras.Seek(0);
                        enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);
                        enc.BitmapTransform.ScaledHeight = (uint)(((double)height / (double)width) * w);
                        enc.BitmapTransform.ScaledWidth = (uint)w;
                        await enc.FlushAsync();
                        img.SetSource(ras);
                    }
                }
            }

            return img;
        }