diff --git a/UICompositionAnimations/Behaviours/AcrylicEffectHelper.cs b/UICompositionAnimations/Behaviours/AcrylicEffectHelper.cs index 838601f..6832375 100644 --- a/UICompositionAnimations/Behaviours/AcrylicEffectHelper.cs +++ b/UICompositionAnimations/Behaviours/AcrylicEffectHelper.cs @@ -6,6 +6,7 @@ using Windows.Foundation; using Windows.Graphics.DirectX; using Windows.Graphics.Effects; +using Windows.Graphics.Imaging; using Windows.UI; using Windows.UI.Composition; using JetBrains.Annotations; @@ -32,6 +33,40 @@ internal static class AcrylicEffectHelper /// private static readonly IDictionary SurfacesCache = new Dictionary(); + /// + /// Loads a from the input , and prepares it for the + /// + /// The resource creator to use to load the image bitmap (it can be the same used later) + /// The compositor instance to use to create the final brush + /// The device to use to process the Win2D image + /// The path to the image to load + private static async Task LoadSurfaceBrushAsync([NotNull] ICanvasResourceCreator creator, + [NotNull] Compositor compositor, [NotNull] CanvasDevice canvasDevice, [NotNull] Uri uri) + { + using (CanvasBitmap bitmap = await CanvasBitmap.LoadAsync(creator, uri)) + { + // Get the device and the target surface + CompositionGraphicsDevice device = CanvasComposition.CreateCompositionGraphicsDevice(compositor, canvasDevice); + CompositionDrawingSurface surface = device.CreateDrawingSurface(default(Size), + DirectXPixelFormat.B8G8R8A8UIntNormalized, DirectXAlphaMode.Premultiplied); + + // Calculate the surface size + Size size = bitmap.Size; + CanvasComposition.Resize(surface, size); + + // Draw the image on the surface and get the resulting brush + using (CanvasDrawingSession session = CanvasComposition.CreateDrawingSession(surface)) + { + session.Clear(Color.FromArgb(0, 0, 0, 0)); + session.DrawImage(bitmap, new Rect(0, 0, size.Width, size.Height), new Rect(0, 0, size.Width, size.Height)); + session.EffectTileSize = new BitmapSize { Width = (uint)size.Width, Height = (uint)size.Height }; + CompositionSurfaceBrush brush = surface.Compositor.CreateSurfaceBrush(surface); + brush.Stretch = CompositionStretch.None; + return brush; + } + } + } + /// /// Loads a instance with the target image /// @@ -50,26 +85,7 @@ async Task LoadImageAsync(bool shouldThrow) // Load the image - this will only succeed when there's an available Win2D device try { - using (CanvasBitmap bitmap = await CanvasBitmap.LoadAsync(canvas, uri)) - { - // Get the device and the target surface - CompositionGraphicsDevice device = CanvasComposition.CreateCompositionGraphicsDevice(compositor, canvas.Device); - CompositionDrawingSurface surface = device.CreateDrawingSurface(default(Size), - DirectXPixelFormat.B8G8R8A8UIntNormalized, DirectXAlphaMode.Premultiplied); - - // Calculate the surface size - Size size = bitmap.Size; - CanvasComposition.Resize(surface, size); - - // Draw the image on the surface and get the resulting brush - using (CanvasDrawingSession session = CanvasComposition.CreateDrawingSession(surface)) - { - session.Clear(Color.FromArgb(0, 0, 0, 0)); - session.DrawImage(bitmap, new Rect(0, 0, size.Width, size.Height), new Rect(0, 0, size.Width, size.Height)); - CompositionSurfaceBrush brush = surface.Compositor.CreateSurfaceBrush(surface); - return brush; - } - } + return await LoadSurfaceBrushAsync(canvas, compositor, canvas.Device, uri); } catch when (!shouldThrow) { @@ -165,25 +181,7 @@ private static async Task LoadWin2DSurfaceBrushFromImag { // This will throw and the canvas will re-initialize the Win2D device if needed CanvasDevice sharedDevice = CanvasDevice.GetSharedDevice(); - using (CanvasBitmap bitmap = await CanvasBitmap.LoadAsync(sharedDevice, uri)) - { - // Get the device and the target surface - CompositionGraphicsDevice device = CanvasComposition.CreateCompositionGraphicsDevice(compositor, sharedDevice); - CompositionDrawingSurface surface = device.CreateDrawingSurface(default(Size), - DirectXPixelFormat.B8G8R8A8UIntNormalized, DirectXAlphaMode.Premultiplied); - - // Calculate the surface size - Size size = bitmap.Size; - CanvasComposition.Resize(surface, size); - - // Draw the image on the surface and get the resulting brush - using (CanvasDrawingSession session = CanvasComposition.CreateDrawingSession(surface)) - { - session.Clear(Color.FromArgb(0, 0, 0, 0)); - session.DrawImage(bitmap, new Rect(0, 0, size.Width, size.Height), new Rect(0, 0, size.Width, size.Height)); - brush = surface.Compositor.CreateSurfaceBrush(surface); - } - } + brush = await LoadSurfaceBrushAsync(sharedDevice, compositor, sharedDevice, uri); } catch {