using System; using System.IO; using System.Threading.Tasks; using Avalonia.Platform; using Avalonia.Visuals.Media.Imaging; namespace Avalonia.Media.Imaging { /// /// Holds a writeable bitmap image. /// public class WriteableBitmap : Bitmap { /// /// Initializes a new instance of the class. /// /// The size of the bitmap in device pixels. /// The DPI of the bitmap. /// The pixel format (optional). /// An . [Obsolete("Use overload taking an AlphaFormat.")] public WriteableBitmap(PixelSize size, Vector dpi, PixelFormat? format = null) : base(CreatePlatformImpl(size, dpi, format, null)) { } /// /// Initializes a new instance of the class. /// /// The size of the bitmap in device pixels. /// The DPI of the bitmap. /// The pixel format (optional). /// The alpha format (optional). /// An . public WriteableBitmap(PixelSize size, Vector dpi, PixelFormat format, AlphaFormat alphaFormat) : base(CreatePlatformImpl(size, dpi, format, alphaFormat)) { } private WriteableBitmap(IWriteableBitmapImpl impl) : base(impl) { } public ILockedFramebuffer Lock() => ((IWriteableBitmapImpl) PlatformImpl.Item).Lock(); public static WriteableBitmap Decode(Stream stream) { var ri = AvaloniaLocator.Current.GetService(); return new WriteableBitmap(ri.LoadWriteableBitmap(stream)); } /// /// Loads a WriteableBitmap from a stream and decodes at the desired width. Aspect ratio is maintained. /// This is more efficient than loading and then resizing. /// /// The stream to read the bitmap from. This can be any supported image format. /// The desired width of the resulting bitmap. /// The to use should any scaling be required. /// An instance of the class. public new static WriteableBitmap DecodeToWidth(Stream stream, int width, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality) { var ri = AvaloniaLocator.Current.GetService(); return new WriteableBitmap(ri.LoadWriteableBitmapToWidth(stream, width, interpolationMode)); } /// /// Loads a Bitmap from a stream and decodes at the desired height. Aspect ratio is maintained. /// This is more efficient than loading and then resizing. /// /// The stream to read the bitmap from. This can be any supported image format. /// The desired height of the resulting bitmap. /// The to use should any scaling be required. /// An instance of the class. public new static WriteableBitmap DecodeToHeight(Stream stream, int height, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality) { var ri = AvaloniaLocator.Current.GetService(); return new WriteableBitmap(ri.LoadWriteableBitmapToHeight(stream, height, interpolationMode)); } private static IBitmapImpl CreatePlatformImpl(PixelSize size, in Vector dpi, PixelFormat? format, AlphaFormat? alphaFormat) { var ri = AvaloniaLocator.Current.GetService(); PixelFormat finalFormat = format ?? ri.DefaultPixelFormat; AlphaFormat finalAlphaFormat = alphaFormat ?? ri.DefaultAlphaFormat; return ri.CreateWriteableBitmap(size, dpi, finalFormat, finalAlphaFormat); } } }