async Task <byte[]> GetImageAsByteAsync(bool usePNG, int quality, int desiredWidth, int desiredHeight) { PImage image = null; await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => { if (Control != null) { image = Control.Image; } }).ConfigureAwait(false); if (image == null) { return(null); } if (desiredWidth != 0 || desiredHeight != 0) { image = image.ResizeUIImage((double)desiredWidth, (double)desiredHeight, InterpolationMode.Default); } #if __IOS__ NSData imageData = usePNG ? image.AsPNG() : image.AsJPEG((nfloat)quality / 100f); if (imageData == null || imageData.Length == 0) { return(null); } var encoded = imageData.ToArray(); imageData.TryDispose(); return(encoded); #elif __MACOS__ byte[] encoded; using (MemoryStream ms = new MemoryStream()) using (var stream = usePNG ? image.AsPngStream() : image.AsJpegStream(quality)) { stream.CopyTo(ms); encoded = ms.ToArray(); } if (desiredWidth != 0 || desiredHeight != 0) { image.TryDispose(); } return(encoded); #endif }