public async Task <byte[]> ExportCurrentImage(int width, int height, float scale, List <Abstractions.Stroke> strokes, Abstractions.Scaling scaling, Abstractions.ExportFormat format, int quality, Painter.Abstractions.Color BackgroundColor, bool useDevicePixelDensity, byte[] BackgroundImage = null) { if (scale == 0) { scale = 1.0f; } //Initialize data holders byte[] data; Stream str = new MemoryStream(); if (useDevicePixelDensity) { width *= (int)metrics.Density; height *= (int)metrics.Density; } Bitmap tempImage = null; Bitmap backgroundBitmap = null; Canvas tempCanvas = null; if (BackgroundImage == null) { tempImage = Bitmap.CreateBitmap(metrics, width, height, Bitmap.Config.Argb8888); tempCanvas = new Canvas(tempImage); } else { backgroundBitmap = await BitmapFactory.DecodeByteArrayAsync(BackgroundImage, 0, BackgroundImage.Length); var backgroundScale = GetDrawingScale(scaling, backgroundBitmap, width, height); tempImage = Bitmap.CreateBitmap(metrics, (int)Math.Min(width, backgroundBitmap.Width * backgroundScale), (int)Math.Min(height, backgroundBitmap.Height * backgroundScale), Bitmap.Config.Argb8888); tempCanvas = new Canvas(tempImage); } DrawStrokes(tempCanvas, strokes, backgroundBitmap, BackgroundColor, scaling, width, height, scale); //Compress the image and save it to the stream switch (format) { case Abstractions.ExportFormat.Png: await tempImage.CompressAsync(CompressFormat.Png, quality, str); break; case Abstractions.ExportFormat.Jpeg: await tempImage.CompressAsync(CompressFormat.Jpeg, quality, str); break; } //Memory management tempImage.Dispose(); tempCanvas.Dispose(); tempImage = null; tempCanvas = null; //Read the data data = new byte[str.Length]; str.Seek(0, SeekOrigin.Begin); await str.ReadAsync(data, 0, (int)str.Length); //Return the data return(data); }
public async Task <byte[]> ExportCurrentImage(int width, int height, float scale, List <Abstractions.Stroke> strokes, Abstractions.Scaling scaling, Abstractions.ExportFormat format, int quality, Painter.Abstractions.Color BackgroundColor, bool useDevicePixelDensity, byte[] BackgroundImage = null) { //useDevicePixelDensity is not used on iOS UIImage image = drawPath(width, height, strokes); NSData imageData; if (format == Abstractions.ExportFormat.Png) { imageData = image.AsPNG(); } else { imageData = image.AsJPEG(quality); } return(imageData.ToArray()); }