public static MemoryStream OverlayAndPickDetailed(MemoryStream sourceStream, int width = 128, int height = 128) { WriteableBitmap source = new WriteableBitmap(UsefulThings.WPF.Images.CreateWPFBitmap(sourceStream)); WriteableBitmap dest = new WriteableBitmap(source.PixelWidth, source.PixelHeight, source.DpiX, source.DpiY, System.Windows.Media.PixelFormats.Bgra32, source.Palette); // KFreon: Write onto black var overlayed = Overlay(dest, source); // KFreon: Choose the most detailed image between one with alpha merged and one without. JpegBitmapEncoder enc = new JpegBitmapEncoder(); enc.QualityLevel = 90; enc.Frames.Add(BitmapFrame.Create(overlayed)); MemoryStream mstest = new MemoryStream(); { enc.Save(mstest); MemoryStream jpg = ImageEngine.GenerateThumbnailToStream(sourceStream, width, height, false); { enc = new JpegBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(new WriteableBitmap(UsefulThings.WPF.Images.CreateWPFBitmap(jpg)))); enc.Save(jpg); MemoryStream largest = jpg.Length > mstest.Length ? jpg : mstest; return(largest); } } }
public static string GenerateThumbnail(Stream sourceStream, string savePath, int maxWidth, int maxHeight) { using (MemoryStream stream = ImageEngine.GenerateThumbnailToStream(sourceStream, maxWidth, maxHeight, false, true)) { if (stream == null) { return(null); } using (MemoryStream largest = OverlayAndPickDetailed(stream, maxWidth, maxHeight)) using (FileStream fs = new FileStream(savePath, FileMode.Create)) largest.WriteTo(fs); return(savePath); } }