From f7792991ee7d73b42fa6a9d8e3f83af75218a414 Mon Sep 17 00:00:00 2001 From: DubyaDude Date: Wed, 20 Mar 2024 21:05:11 -0400 Subject: [PATCH] Added ConvertToPng to helper method --- Sample.UI/MainWindow.xaml.cs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/Sample.UI/MainWindow.xaml.cs b/Sample.UI/MainWindow.xaml.cs index ca9bfaa..d6bd216 100644 --- a/Sample.UI/MainWindow.xaml.cs +++ b/Sample.UI/MainWindow.xaml.cs @@ -1,5 +1,6 @@ using ModernWpf.Controls; using System; +using System.Drawing; using System.Windows; using System.Windows.Media.Imaging; using Windows.Media.Control; @@ -172,27 +173,43 @@ private async void Forward_Click(object sender, RoutedEventArgs e) internal static class Helper { - internal static BitmapImage? GetThumbnail(IRandomAccessStreamReference Thumbnail) + internal static BitmapImage? GetThumbnail(IRandomAccessStreamReference Thumbnail, bool convertToPng = false) { if (Thumbnail == null) return null; - var imageStream = Thumbnail.OpenReadAsync().GetAwaiter().GetResult(); - byte[] fileBytes = new byte[imageStream.Size]; - using (DataReader reader = new DataReader(imageStream)) + var thumbnailStream = Thumbnail.OpenReadAsync().GetAwaiter().GetResult(); + byte[] thumbnailBytes = new byte[thumbnailStream.Size]; + using (DataReader reader = new DataReader(thumbnailStream)) { - reader.LoadAsync((uint)imageStream.Size).GetAwaiter().GetResult(); - reader.ReadBytes(fileBytes); + reader.LoadAsync((uint)thumbnailStream.Size).GetAwaiter().GetResult(); + reader.ReadBytes(thumbnailBytes); + } + + byte[] imageBytes = thumbnailBytes; + + if (convertToPng) + { + using (var fileMemoryStream = new System.IO.MemoryStream(thumbnailBytes)) + { + Bitmap b = (Bitmap)Bitmap.FromStream(fileMemoryStream); + using (var pngMemoryStream = new System.IO.MemoryStream()) + { + b.Save(pngMemoryStream, System.Drawing.Imaging.ImageFormat.Png); + imageBytes = pngMemoryStream.ToArray(); + } + } } var image = new BitmapImage(); - using (var ms = new System.IO.MemoryStream(fileBytes)) + using (var ms = new System.IO.MemoryStream(imageBytes)) { image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = ms; image.EndInit(); } + return image; } }