private static async Task <IStorageFile> SaveThumbnail(int id, DjvuDocument document) { var page = await document.GetPageAsync(1); var maxWidth = 140 * DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; var aspectRatio = (double)page.Width / page.Height; var width = (uint)Math.Min(maxWidth, page.Width); var height = (uint)(width / aspectRatio); var bitmap = new WriteableBitmap((int)width, (int)height); await page.RenderRegionAsync( bitmap : bitmap, rescaledPageSize : new BitmapSize { Width = width, Height = height }, renderRegion : new BitmapBounds { Width = width, Height = height }); var booksFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Books", CreationCollisionOption.OpenIfExists); var thumbnailFile = await booksFolder.CreateFileAsync($"{id}.jpg", CreationCollisionOption.ReplaceExisting); using (var thumbnailStream = await thumbnailFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, thumbnailStream); encoder.SetPixelData( pixelFormat: BitmapPixelFormat.Bgra8, alphaMode: BitmapAlphaMode.Ignore, width: (uint)bitmap.PixelWidth, height: (uint)bitmap.PixelHeight, dpiX: 96.0, dpiY: 96.0, pixels: bitmap.PixelBuffer.ToArray()); await encoder.FlushAsync(); } return(thumbnailFile); }