using System.IO; using System.Windows.Media; using System.Windows.Media.Imaging; namespace Azaion.Common.Extensions; public static class BitmapExtensions { public static async Task OpenImage(this string imagePath) { await using var stream = File.OpenRead(imagePath); return OpenImage(stream); } public static BitmapImage OpenImage(this Stream stream) { stream.Seek(0, SeekOrigin.Begin); var image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream; image.EndInit(); image.Freeze(); return image; } public static Color CreateTransparent(this Color color, byte transparency) => Color.FromArgb(transparency, color.R, color.G, color.B); public static async Task SaveImage(this BitmapSource bitmap, string path, CancellationToken ct = default) { await using var stream = new FileStream(path, FileMode.Create); var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(stream); await stream.FlushAsync(ct); } }