From def618eac3990f5398b55ddcf23defd1edf7d284 Mon Sep 17 00:00:00 2001 From: dimonovdd Date: Sun, 14 Feb 2021 23:14:49 +0300 Subject: [PATCH] Ability to set backgroundColor of a image --- ImageFromXamarinUI/ImageFromXamarinUI.csproj | 11 ++++------ .../VisualElementExtension.android.cs | 8 ++++---- .../VisualElementExtension.ios.cs | 20 ++++++++++++++----- .../VisualElementExtension.netstandard.cs | 2 +- .../VisualElementExtension.shared.cs | 6 ++++-- README.md | 2 +- Sample/Sample/MainPage.xaml | 9 ++++++--- Sample/Sample/MainViewModel.cs | 19 ++++++++++++++++-- 8 files changed, 52 insertions(+), 25 deletions(-) diff --git a/ImageFromXamarinUI/ImageFromXamarinUI.csproj b/ImageFromXamarinUI/ImageFromXamarinUI.csproj index b7280cf..c303958 100644 --- a/ImageFromXamarinUI/ImageFromXamarinUI.csproj +++ b/ImageFromXamarinUI/ImageFromXamarinUI.csproj @@ -13,33 +13,30 @@ 1.0.0.0 1.0.0.0 1.0.0 - 1.0.0-pre1 + 1.0.0-pre2 dimonovdd dimonovdd https://github.com/dimonovdd/ImageFromXamarinUI See: https://github.com/dimonovdd/ImageFromXamarinUI/releases $(DefineConstants); - false false LICENSE true https://github.com/dimonovdd/ImageFromXamarinUI portable - Debug;Release + Debug;Release + 8.0 true - - bin\Debug\netstandard2.0\ImageFromXamarinUI.xml - bin\Release\netstandard2.0\ImageFromXamarinUI.xml - + diff --git a/ImageFromXamarinUI/VisualElementExtension.android.cs b/ImageFromXamarinUI/VisualElementExtension.android.cs index 0b71068..f06e6c2 100644 --- a/ImageFromXamarinUI/VisualElementExtension.android.cs +++ b/ImageFromXamarinUI/VisualElementExtension.android.cs @@ -9,9 +9,9 @@ namespace ImageFromXamarinUI { public static partial class VisualElementExtension { - static async Task PlatformCaptureImageAsync(VisualElement view) + static async Task PlatformCaptureImageAsync(VisualElement view, Xamarin.Forms.Color backgroundColor) { - using var bitmap = ViewToBitMap(GetNativeView(view)); + using var bitmap = ViewToBitMap(GetNativeView(view), backgroundColor); var stream = await BitMapToStream(bitmap); bitmap.Recycle(); return stream; @@ -27,11 +27,11 @@ static Android.Views.View GetNativeView(VisualElement view) return render.View; } - static Bitmap ViewToBitMap(Android.Views.View view) + static Bitmap ViewToBitMap(Android.Views.View view, Xamarin.Forms.Color backgroundColor) { var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888); using var canvas = new Canvas(bitmap); - canvas.DrawColor(Android.Graphics.Color.Transparent); + canvas.DrawColor(backgroundColor.ToAndroid()); view.Draw(canvas); return bitmap; diff --git a/ImageFromXamarinUI/VisualElementExtension.ios.cs b/ImageFromXamarinUI/VisualElementExtension.ios.cs index 9b35f4e..dbf0722 100644 --- a/ImageFromXamarinUI/VisualElementExtension.ios.cs +++ b/ImageFromXamarinUI/VisualElementExtension.ios.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Threading.Tasks; +using CoreGraphics; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; @@ -9,10 +10,10 @@ namespace ImageFromXamarinUI { public static partial class VisualElementExtension { - static Task PlatformCaptureImageAsync(VisualElement view) + static Task PlatformCaptureImageAsync(VisualElement view, Color backgroundColor) { Stream stream = null; - using (var image = ViewToUIImage(GetNativeView(view))) + using (var image = ViewToUIImage(GetNativeView(view), backgroundColor)) stream = ImageToStream(image); return Task.FromResult(stream); } @@ -27,10 +28,19 @@ static UIView GetNativeView(VisualElement view) return render.NativeView; } - static UIImage ViewToUIImage(UIView view) + static UIImage ViewToUIImage(UIView view, Color backgroundColor) { - UIGraphics.BeginImageContextWithOptions(view.Frame.Size, false, UIScreen.MainScreen.Scale); - view.Layer.RenderInContext(UIGraphics.GetCurrentContext()); + var size = view.Frame.Size; + UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale); + using var context = UIGraphics.GetCurrentContext(); + + if(backgroundColor != Color.Transparent) + { + context.SetFillColor(backgroundColor.ToCGColor()); + context.FillRect(new CGRect(0, 0, size.Width, size.Height)); + } + + view.Layer.RenderInContext(context); var image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); diff --git a/ImageFromXamarinUI/VisualElementExtension.netstandard.cs b/ImageFromXamarinUI/VisualElementExtension.netstandard.cs index ee5e8aa..9e97646 100644 --- a/ImageFromXamarinUI/VisualElementExtension.netstandard.cs +++ b/ImageFromXamarinUI/VisualElementExtension.netstandard.cs @@ -7,7 +7,7 @@ namespace ImageFromXamarinUI { public static partial class VisualElementExtension { - static Task PlatformCaptureImageAsync(VisualElement view) + static Task PlatformCaptureImageAsync(VisualElement view, Color backgroundColor) => throw new NotImplementedException($"{nameof(CaptureImageAsync)} not supported on netstandart project"); } diff --git a/ImageFromXamarinUI/VisualElementExtension.shared.cs b/ImageFromXamarinUI/VisualElementExtension.shared.cs index 54ec69c..1707de9 100644 --- a/ImageFromXamarinUI/VisualElementExtension.shared.cs +++ b/ImageFromXamarinUI/VisualElementExtension.shared.cs @@ -4,12 +4,14 @@ namespace ImageFromXamarinUI { + /// The extension class with methods for creating images public static partial class VisualElementExtension { /// Captures an image from the current state of /// element to which the render was assigned + /// /// Stream an image as png with transparency - public static async Task CaptureImageAsync(this VisualElement element) - => await PlatformCaptureImageAsync(element); + public static async Task CaptureImageAsync(this VisualElement element, Color? backgroundColor = null) + => await PlatformCaptureImageAsync(element, backgroundColor ?? Color.Transparent); } } \ No newline at end of file diff --git a/README.md b/README.md index 872fba6..0778123 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ async void OnCapture(Xamarin.Forms.VisualElement element) { try { - var stream = await element.CaptureImageAsync(); + var stream = await element.CaptureImageAsync(Color.White); ResultImageSource = ImageSource.FromStream(() => stream); } catch (Exception) diff --git a/Sample/Sample/MainPage.xaml b/Sample/Sample/MainPage.xaml index 78b0401..9324206 100644 --- a/Sample/Sample/MainPage.xaml +++ b/Sample/Sample/MainPage.xaml @@ -10,7 +10,7 @@ - + -