private void SamplePage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // Get backing visual from shadow container and interop compositor _shadowContainer = ElementCompositionPreview.GetElementVisual(ShadowContainer); _compositor = _shadowContainer.Compositor; // Get CompositionImage, its sprite visual _image = VisualTreeHelperExtensions.GetFirstDescendantOfType <CompositionImage>(ShadowContainer); _imageVisual = _image.SpriteVisual; // Load mask asset onto surface using helpers in SamplesCommon _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor); _imageMaskSurface = _imageLoader.CreateCircleSurface(200, Colors.White); // Create surface brush for mask CompositionSurfaceBrush mask = _compositor.CreateSurfaceBrush(); mask.Surface = _imageMaskSurface.Surface; // Get surface brush from composition image CompositionSurfaceBrush source = _image.SurfaceBrush as CompositionSurfaceBrush; // Create mask brush for toggle mask functionality _maskBrush = _compositor.CreateMaskBrush(); _maskBrush.Mask = mask; _maskBrush.Source = source; // Initialize toggle mask _isMaskEnabled = false; }
private void SamplePage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // Get backing visual from shadow container and interop compositor _shadowContainer = ElementCompositionPreview.GetElementVisual(ShadowContainer); _compositor = _shadowContainer.Compositor; // Get CompositionImage, its sprite visual _image = VisualTreeHelperExtensions.GetFirstDescendantOfType<CompositionImage>(ShadowContainer); _imageVisual = _image.SpriteVisual; // Load mask asset onto surface using helpers in SamplesCommon _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor); _imageMaskSurface = _imageLoader.CreateCircleSurface(200, Colors.White); // Create surface brush for mask CompositionSurfaceBrush mask = _compositor.CreateSurfaceBrush(); mask.Surface = _imageMaskSurface.Surface; // Get surface brush from composition image CompositionSurfaceBrush source = _image.SurfaceBrush as CompositionSurfaceBrush; // Create mask brush for toggle mask functionality _maskBrush = _compositor.CreateMaskBrush(); _maskBrush.Mask = mask; _maskBrush.Source = source; // Initialize toggle mask _isMaskEnabled = false; }
/// <summary> /// Creates the circle used in the circle brush if not already created. /// </summary> private void EnsureCircleBrush() { if (_circleBrush == null) { _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor); _circleSurface = _imageLoader.CreateCircleSurface(200, Colors.White); _circleBrush = _compositor.CreateSurfaceBrush(_circleSurface.Surface); } }
public ColorBloomTransitionHelper(UIElement hostForVisual) { this.hostForVisual = hostForVisual; var visual = ElementCompositionPreview.GetElementVisual(hostForVisual); _compositor = visual.Compositor; _containerForVisuals = _compositor.CreateContainerVisual(); ElementCompositionPreview.SetElementChildVisual(hostForVisual, _containerForVisuals); _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor); _circleMaskSurface = _imageLoader.CreateCircleSurface(200, Colors.White); }
/// <summary> /// Creates an instance of the ColorBloomTransitionHelper. /// Any visuals to be later created and animated will be hosted within the specified UIElement. /// </summary> public ColorBloomTransitionHelper(UIElement hostForVisual) { this.hostForVisual = hostForVisual; // we have an element in the XAML tree that will host our Visuals var visual = ElementCompositionPreview.GetElementVisual(hostForVisual); _compositor = visual.Compositor; // create a container // adding children to this container adds them to the live visual tree _containerForVisuals = _compositor.CreateContainerVisual(); ElementCompositionPreview.SetElementChildVisual(hostForVisual, _containerForVisuals); // initialize the ImageLoader and create the circle mask _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor); _circleMaskSurface = _imageLoader.CreateCircleSurface(200, Colors.White); }
/// <summary> /// Creates an instance of the ColorBloomTransitionHelper. /// Any visuals to be later created and animated will be hosted within the specified UIElement. /// </summary> public ColorBloomTransitionHelper(UIElement hostForVisual) { this.hostForVisual = hostForVisual; // we have an element in the XAML tree that will host our Visuals var visual = ElementCompositionPreview.GetElementVisual(hostForVisual); _compositor = visual.Compositor; // create a container // adding children to this container adds them to the live visual tree _containerForVisuals = _compositor.CreateContainerVisual(); ElementCompositionPreview.SetElementChildVisual(hostForVisual, _containerForVisuals); // initialize the ImageLoader and create the circle mask _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor); _circleMaskSurface = _imageLoader.CreateCircleSurface(200, Colors.White); }
/// <summary> /// Updates the effects applied and visuals shown based on information retrieved from /// calling the capabilities API /// </summary> private void UpdateAlbumArt() { if (_capabilities.AreEffectsSupported()) { // // If effects are supported, add effects to the background image and // add a masked circle image in the center for better visual appearance. // if (!_containsCircleImage) { // Create circle mask var circleMaskSurface = _imageLoader.CreateCircleSurface(200, Colors.White); // Create image visual to use as the circle-masked center image _circleImageVisual = _compositor.CreateSpriteVisual(); _circleImageVisual.Size = new Vector2((float)ImageCanvas.ActualWidth / 2, (float)ImageCanvas.ActualHeight / 2); var xOffset = (float)(ImageCanvas.ActualWidth / 2 - _circleImageVisual.Size.X / 2); var yOffset = (float)(ImageCanvas.ActualHeight / 2 - _circleImageVisual.Size.Y / 2); _circleImageVisual.Offset = new Vector3(xOffset, yOffset, 0); // Create circle image surface CompositionSurfaceBrush circleSurfaceBrush = _compositor.CreateSurfaceBrush(); circleSurfaceBrush.Surface = circleMaskSurface.Surface; circleSurfaceBrush.Stretch = CompositionStretch.Uniform; // Apply mask to visual CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush(); maskBrush.Source = _imageSurfaceBrush; maskBrush.Mask = circleSurfaceBrush; _circleImageVisual.Brush = maskBrush; _imageContainer.Children.InsertAtTop(_circleImageVisual); _containsCircleImage = true; } // // Create saturation effect, which will be either used alone if effects are slow, or chained // with blur if effects are fast // var saturationEffect = new SaturationEffect { Saturation = 0.3f, Source = new CompositionEffectSourceParameter("SaturationSource") }; if (_capabilities.AreEffectsFast()) { // Create blur effect and chain with saturation effect GaussianBlurEffect chainedEffect = new GaussianBlurEffect() { Name = "Blur", Source = saturationEffect, //takes saturation effect as input BlurAmount = 6.0f, BorderMode = EffectBorderMode.Hard, Optimization = EffectOptimization.Balanced }; CompositionEffectFactory chainedEffectFactory = _compositor.CreateEffectFactory(chainedEffect); CompositionEffectBrush effectBrush = chainedEffectFactory.CreateBrush(); effectBrush.SetSourceParameter("SaturationSource", _imageSurfaceBrush); _backgroundImageVisual.Brush = effectBrush; CapabilityText = "Effects are supported and fast. Background image is blurred and desaturated."; } else { // If effects are slow but supported use desaturation effect since it is less expensive than blur CompositionEffectFactory saturationEffectFactory = _compositor.CreateEffectFactory(saturationEffect); CompositionEffectBrush saturationBrush = saturationEffectFactory.CreateBrush(); saturationBrush.SetSourceParameter("SaturationSource", _imageSurfaceBrush); _backgroundImageVisual.Brush = saturationBrush; CapabilityText = "Effects are supported but not fast. Background image is desaturated."; } } else { // // If effects are not supported, just use the image as the background with no effects // and remove the center circle image to declutter the UI. // if (_containsCircleImage) { _imageContainer.Children.Remove(_circleImageVisual); _containsCircleImage = false; } _backgroundImageVisual.Brush = _imageSurfaceBrush; CapabilityText = "Effects not supported. No effects are applied."; } }
/// <summary> /// Creates the circle used in the circle brush if not already created. /// </summary> private void EnsureCircleBrush() { if (_circleBrush == null) { _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor); _circleSurface = _imageLoader.CreateCircleSurface(200, Colors.White); _circleBrush = _compositor.CreateSurfaceBrush(_circleSurface.Surface); } }