public Task Save(CompressedBitmapFormat format, float quality, Stream target) { return Task.Run(() => { #if UIKIT var data = format == CompressedBitmapFormat.Jpeg ? inner.AsJPEG((float)quality) : inner.AsPNG(); data.AsStream().CopyTo(target); #else #if UNIFIED var rect = new CoreGraphics.CGRect(); #else var rect = new RectangleF(); #endif var cgImage = inner.AsCGImage(ref rect, null, null); var imageRep = new NSBitmapImageRep(cgImage); var props = format == CompressedBitmapFormat.Png ? new NSDictionary() : new NSDictionary(new NSNumber(quality), new NSString("NSImageCompressionFactor")); var type = format == CompressedBitmapFormat.Png ? NSBitmapImageFileType.Png : NSBitmapImageFileType.Jpeg; var outData = imageRep.RepresentationUsingTypeProperties(type, props); outData.AsStream().CopyTo(target); #endif }); }
/// <summary> /// Saves the specified color data as an image with the specified format. /// </summary> /// <param name="data">An array containing the image's color data.</param> /// <param name="width">The width of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param> /// <param name="stream">The stream to which to save the image data.</param> /// <param name="format">The format with which to save the image.</param> private void Save(Color[] data, Int32 width, Int32 height, Stream stream, ImageFormat format) { using (var rep = new NSBitmapImageRep(IntPtr.Zero, width, height, 8, 4, true, false, "NSCalibratedRGBColorSpace", 0, 0)) { fixed (Color* pData = data) { for (int y = 0; y < height; y++) { var pSrc = pData + (y * width); var pDst = (UInt32*)((Byte*)rep.BitmapData + (y * rep.BytesPerRow)); for (int x = 0; x < width; x++) { *pDst++ = (*pSrc++).ToArgb(); } } } var filetype = (format == ImageFormat.Png) ? NSBitmapImageFileType.Png : NSBitmapImageFileType.Jpeg; var properties = new NSDictionary(); using (var imgData = rep.RepresentationUsingTypeProperties(filetype, properties)) { using (var imgStream = imgData.AsStream()) { imgStream.CopyTo(stream); } } } }