using Sandbox.Utility; using SkiaSharp; namespace Sandbox; public partial class Bitmap { /// /// Exports the bitmap as a JPEG byte array with the specified quality. /// /// The quality of the JPEG, between 0 and 100. /// A byte array containing the JPEG image data. public byte[] ToJpg( int quality = 100 ) { return Encode( SKEncodedImageFormat.Jpeg, quality ); } /// /// Exports the bitmap as a PNG byte array. /// /// A byte array containing the PNG image data. public byte[] ToPng() { return Encode( SKEncodedImageFormat.Png, 100 ); } /// /// Exports the bitmap as a BMP byte array. /// /// A byte array containing the BMP image data. public byte[] ToBmp() { return Encode( SKEncodedImageFormat.Bmp, 100 ); } /// /// Exports the bitmap as an HDR WebP byte array with the specified quality. /// /// The quality of the WebP image, between 0 and 100. /// A byte array containing the WebP HDR image data. public byte[] ToWebP( int quality = 100 ) { return Encode( SKEncodedImageFormat.Webp, quality ); } /// /// Exports the bitmap to the specified image format with optional quality. /// /// The image format (e.g., PNG, JPEG, BMP). /// The quality of the image, used for formats like JPEG. /// A byte array containing the image data. private byte[] Encode( SKEncodedImageFormat format, int quality ) { using var image = SKImage.FromBitmap( _bitmap ); using var data = image.Encode( format, quality ); return data.ToArray(); } /// /// Exports the bitmap to the specified engine format /// public byte[] ToFormat( ImageFormat format ) { var data = _bitmap.GetPixels(); using var fbm = new FloatBitmap( Width, Height, ImageFormat, data, ByteCount ); return fbm.EncodeTo( format ); } }