Skip to content

Commit

Permalink
Added ConvertToPng to helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
DubyaDude committed Mar 21, 2024
1 parent f5a6366 commit f779299
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions Sample.UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ModernWpf.Controls;
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Media.Imaging;
using Windows.Media.Control;
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit f779299

Please sign in to comment.