public async Task <WriteableBitmap> GetImageFromUrl(string url) { var bytes = new HttpClient().GetByteArrayAsync(url).Result; // Create an instance of the decoder var webp = new WebPDecoder(); // Decode to BGRA (Bitmaps use this format) var pixelData1 = await webp.DecodeBgraAsync(bytes); var pixelData = pixelData1.ToArray(); // Get the size var size = await webp.GetSizeAsync(bytes); // With the size of the webp, create a WriteableBitmap var bitmap = new WriteableBitmap((int)size.Width, (int)size.Height); // Write the pixel data to the buffer var stream = bitmap.PixelBuffer.AsStream(); await stream.WriteAsync(pixelData, 0, pixelData.Length); return(bitmap); }
private static async void SourceCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { var image = d as Image; var imageBrush = d as ImageBrush; if (imageBrush == null && image == null) return; if (image != null) image.Source = null; else imageBrush.ImageSource = null; var url = (Uri) e.NewValue; if (url == null) { return; } if (!url.OriginalString.Contains(".webp")) { if (image != null) image.Source = new BitmapImage(url); else imageBrush.ImageSource = new BitmapImage(url); return; } if (url.OriginalString.StartsWith("ms-resource")) url = new Uri(url.OriginalString.Replace("ms-resource:///Files/", "ms-appx:///")); var streamReference = RandomAccessStreamReference.CreateFromUri(url); ImageSource source; byte[] arr; using (var stream = (await streamReference.OpenReadAsync()).AsStreamForRead()) { arr = new byte[stream.Length]; await stream.ReadAsync(arr, 0, arr.Length); } try { var webp = new WebPDecoder(); var size = await webp.GetSizeAsync(arr); var pixelData = await webp.DecodeBgraAsync(arr); var wb = new WriteableBitmap((int) size.Width, (int) size.Height); var pixelArr = pixelData.ToArray(); using (var pixelDataStream = wb.PixelBuffer.AsStream()) { await pixelDataStream.WriteAsync(pixelArr, 0, pixelArr.Length); source = wb; } } catch { using (var rnd = new MemoryStream(arr).AsRandomAccessStream()) { var bitmap = new BitmapImage(); await bitmap.SetSourceAsync(rnd); source = bitmap; } } if (image != null) image.Source = source; else imageBrush.ImageSource = source; } catch (Exception ex) { Debug.WriteLine("Something happened opening webp image. Stack trace: " + ex.Message); } }
private static async void SourceCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { var image = d as Image; var imageBrush = d as ImageBrush; if (imageBrush == null && image == null) { return; } if (image != null) { image.Source = null; } else { imageBrush.ImageSource = null; } var url = (Uri)e.NewValue; if (url == null) { return; } if (!url.OriginalString.Contains(".webp")) { if (image != null) { image.Source = new BitmapImage(url); } else { imageBrush.ImageSource = new BitmapImage(url); } return; } if (url.OriginalString.StartsWith("ms-resource")) { url = new Uri(url.OriginalString.Replace("ms-resource:///Files/", "ms-appx:///")); } var streamReference = RandomAccessStreamReference.CreateFromUri(url); ImageSource source; byte[] arr; using (var stream = (await streamReference.OpenReadAsync()).AsStreamForRead()) { arr = new byte[stream.Length]; await stream.ReadAsync(arr, 0, arr.Length); } try { var webp = new WebPDecoder(); var size = await webp.GetSizeAsync(arr); var pixelData = await webp.DecodeBgraAsync(arr); var wb = new WriteableBitmap((int)size.Width, (int)size.Height); var pixelArr = pixelData.ToArray(); using (var pixelDataStream = wb.PixelBuffer.AsStream()) { await pixelDataStream.WriteAsync(pixelArr, 0, pixelArr.Length); source = wb; } } catch { using (var rnd = new MemoryStream(arr).AsRandomAccessStream()) { var bitmap = new BitmapImage(); await bitmap.SetSourceAsync(rnd); source = bitmap; } } if (image != null) { image.Source = source; } else { imageBrush.ImageSource = source; } } catch (Exception ex) { Debug.WriteLine("Something happened opening webp image. Stack trace: " + ex.Message); } }
public async Task<WriteableBitmap> GetImageFromUrl(string url) { var bytes = new HttpClient().GetByteArrayAsync(url).Result; // Create an instance of the decoder var webp = new WebPDecoder(); // Decode to BGRA (Bitmaps use this format) var pixelData1 = await webp.DecodeBgraAsync(bytes); var pixelData = pixelData1.ToArray(); // Get the size var size = await webp.GetSizeAsync(bytes); // With the size of the webp, create a WriteableBitmap var bitmap = new WriteableBitmap((int)size.Width, (int)size.Height); // Write the pixel data to the buffer var stream = bitmap.PixelBuffer.AsStream(); await stream.WriteAsync(pixelData, 0, pixelData.Length); return bitmap; }