diff --git a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.Android.cs b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.Android.cs index 5b1c2300a121..af407d2b88e0 100644 --- a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.Android.cs +++ b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.Android.cs @@ -50,19 +50,16 @@ private protected override bool TryOpenSourceSync(int? targetWidth, int? targetH return image != null; } - private (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null) + private (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null) { var byteCount = 0; Bitmap? bitmap = default; - UIElement elementToRender = element - ?? XamlRoot.Current.Content - ?? throw new global::System.NullReferenceException(); // In UWP RenderTargetBitmap use logical size // if using logical size to render the element there are generate glycth. // to avoid this layout the control on Physical and after scale to logical - var logical = elementToRender.ActualSize.ToSize(); + var logical = element.ActualSize.ToSize(); var physical = logical.LogicalToPhysicalPixels(); if (physical.IsEmpty) { @@ -70,7 +67,7 @@ private protected override bool TryOpenSourceSync(int? targetWidth, int? targetH } try { - SetSoftwareRendering(elementToRender, true); + SetSoftwareRendering(element, true); bitmap = Bitmap.CreateBitmap((int)physical.Width, (int)physical.Height, Bitmap.Config.Argb8888!, true) ?? throw new InvalidOperationException("Failed to create target native bitmap."); using var canvas = new Canvas(bitmap); @@ -79,7 +76,7 @@ private protected override bool TryOpenSourceSync(int? targetWidth, int? targetH Uno.UI.UnoViewGroup.TryFastRequestLayout(element, false); // Render on the canvas canvas.DrawColor(Colors.Transparent, PorterDuff.Mode.Clear!); - elementToRender.Draw(canvas); + element.Draw(canvas); } var targetSize = scaledSize ?? logical; @@ -111,7 +108,7 @@ private protected override bool TryOpenSourceSync(int? targetWidth, int? targetH finally { bitmap?.Dispose(); - SetSoftwareRendering(elementToRender, false); + SetSoftwareRendering(element, false); } diff --git a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.cs b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.cs index 2d7549b3026a..3bbbcfb68565 100644 --- a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.cs +++ b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.cs @@ -87,7 +87,11 @@ public IAsyncAction RenderAsync(UIElement? element, int scaledWidth, int scaledH { try { - (_bufferSize, PixelWidth, PixelHeight) = RenderAsBgra8_Premul(element, ref _buffer, new Size(scaledWidth, scaledHeight)); + UIElement elementToRender = element + ?? XamlRoot.Current.Content + ?? throw new global::System.NullReferenceException("Invalid XamlRoot Content."); + + (_bufferSize, PixelWidth, PixelHeight) = RenderAsBgra8_Premul(elementToRender, ref _buffer, new Size(scaledWidth, scaledHeight)); #if __WASM__ || __SKIA__ InvalidateSource(); #endif @@ -106,7 +110,11 @@ public IAsyncAction RenderAsync(UIElement? element) { try { - (_bufferSize, PixelWidth, PixelHeight) = RenderAsBgra8_Premul(element, ref _buffer); + UIElement elementToRender = element + ?? XamlRoot.Current.Content + ?? throw new global::System.NullReferenceException("Invalid XamlRoot Content."); + + (_bufferSize, PixelWidth, PixelHeight) = RenderAsBgra8_Premul(elementToRender, ref _buffer); #if __WASM__ || __SKIA__ InvalidateSource(); #endif @@ -131,7 +139,7 @@ public IAsyncOperation GetPixelsAsync() }); #if NOT_IMPLEMENTED - private (int ByteCount,int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null) + private (int ByteCount,int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null) => throw new NotImplementedException("RenderTargetBitmap is not supported on this platform."); #endif diff --git a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.iOS.cs b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.iOS.cs index e5b1c994ee82..590570be1fc9 100644 --- a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.iOS.cs +++ b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.iOS.cs @@ -44,12 +44,9 @@ private protected override bool TryOpenSourceSync([NotNullWhen(true)] out UIImag return image != null; } - private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null) + private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null) { - UIElement elementToRender = element - ?? XamlRoot.Current.Content - ?? throw new global::System.NullReferenceException(); - var size = new Size(elementToRender.ActualSize.X, elementToRender.ActualSize.Y); + var size = new Size(element.ActualSize.X, element.ActualSize.Y); if (size.IsEmpty) { @@ -61,7 +58,7 @@ private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIEle UIGraphics.BeginImageContextWithOptions(size, false, 1f); var ctx = UIGraphics.GetCurrentContext(); ctx.SetFillColor(Colors.Transparent); // This is only for pixels not used, but the bitmap as the same size of the element. We keep it only for safety! - elementToRender.Layer.RenderInContext(ctx); + element.Layer.RenderInContext(ctx); img = UIGraphics.GetImageFromCurrentImageContext(); } finally diff --git a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.macOS.cs b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.macOS.cs index 17a19c30ce60..7824235fdc0c 100644 --- a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.macOS.cs +++ b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.macOS.cs @@ -46,13 +46,9 @@ private protected override bool TryOpenSourceSync([NotNullWhen(true)] out NSImag return image != null; } - private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null) + private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null) { - UIElement elementToRender = element - ?? XamlRoot.Current.Content - ?? throw new global::System.NullReferenceException(); - - var size = new Size(elementToRender.ActualSize.X, elementToRender.ActualSize.Y); + var size = new Size(element.ActualSize.X, element.ActualSize.Y); if (size.IsEmpty) { @@ -62,10 +58,10 @@ private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIEle try { img = new NSImage(size); - img.LockFocusFlipped(elementToRender.IsFlipped); + img.LockFocusFlipped(element.IsFlipped); var ctx = NSGraphicsContext.CurrentContext!.GraphicsPort; ctx.SetFillColor(Colors.Transparent); // This is only for pixels not used, but the bitmap as the same size of the element. We keep it only for safety! - elementToRender.Layer!.RenderInContext(ctx); + element.Layer!.RenderInContext(ctx); } finally { diff --git a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.skia.cs b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.skia.cs index 5a153d7e9b39..3fd7f8356171 100644 --- a/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.skia.cs +++ b/src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.skia.cs @@ -11,17 +11,13 @@ partial class RenderTargetBitmap ? new SwapColor(SwapRB) : default(SwapColor); - private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null) + private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null) { - UIElement elementToRender = element - ?? XamlRoot.Current.Content - ?? throw new global::System.NullReferenceException(); + var renderSize = element.RenderSize; + var visual = element.Visual; - var renderSize = elementToRender.RenderSize; - var visual = elementToRender.Visual; - - if (elementToRender.RenderSize is { IsEmpty: true } - || elementToRender.RenderSize is { Width: 0, Height: 0 }) + if (element.RenderSize is { IsEmpty: true } + || element.RenderSize is { Width: 0, Height: 0 }) { return (0, 0, 0); }