Example #1
0
        private async void InitComposition()
        {
            // Store our Compositor and create our ImageLoader.
            _compositor  = ElementCompositionPreview.GetElementVisual(this).Compositor;
            _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor);

            // Setup our effect definition. First is the CompositeEffect that will take
            // our sources and produce the intersection of the images (because we selected
            // the DestinationIn mode for the effect). Next we take our CompositeEffect
            // and make it the source of our next effect, the InvertEffect. This will take
            // the intersection image and invert the colors. Finally we take that combined
            // effect and put it through a HueRotationEffect, were we can adjust the colors
            // using the Angle property (which we will animate below).
            IGraphicsEffect graphicsEffect = new HueRotationEffect
            {
                Name   = "hueEffect",
                Angle  = 0.0f,
                Source = new InvertEffect
                {
                    Source = new CompositeEffect
                    {
                        Mode    = Microsoft.Graphics.Canvas.CanvasComposite.DestinationIn,
                        Sources =
                        {
                            new CompositionEffectSourceParameter("image"),
                            new CompositionEffectSourceParameter("mask")
                        }
                    }
                }
            };

            // Create our effect factory using the effect definition and mark the Angle
            // property as adjustable/animatable.
            _effectFactory = _compositor.CreateEffectFactory(graphicsEffect, new string[] { "hueEffect.Angle" });

            // Create MangedSurfaces for both our base image and the mask we'll be using.
            // The mask is a transparent image with a white circle in the middle. This is
            // important since the CompositeEffect will use just the circle for the
            // intersectionsince the rest is transparent.
            var managedImageSurface = await _imageLoader.CreateManagedSurfaceFromUriAsync(new Uri("ms-appx:///Assets/tripphoto1.jpg"));

            var managedMaskSurface = await _imageLoader.CreateManagedSurfaceFromUriAsync(new Uri("ms-appx:///Assets/CircleMask.png"));

            // Create brushes from our surfaces.
            var imageBrush = _compositor.CreateSurfaceBrush(managedImageSurface.Surface);
            var maskBrush  = _compositor.CreateSurfaceBrush(managedMaskSurface.Surface);

            // Create an setup our effect brush.Assign both the base image and mask image
            // brushes as source parameters in the effect (with the same names we used in
            // the effect definition). If we wanted, we could create many effect brushes
            // and use different images in all of them.
            var effectBrush = _effectFactory.CreateBrush();

            effectBrush.SetSourceParameter("image", imageBrush);
            effectBrush.SetSourceParameter("mask", maskBrush);

            // All that's left is to create a visual, assign the effect brush to the Brush
            // property, and attach it into the tree...
            var visual = _compositor.CreateSpriteVisual();

            visual.Size   = managedImageSurface.Size.ToVector2();
            visual.Offset = new Vector3(50, 50, 0);

            visual.Brush = effectBrush;

            ElementCompositionPreview.SetElementChildVisual(this, visual);

            // ... but wait! There's more! We're going to animate the Angle property
            // to get a really trippy color effect on our masked inverted image.
            AnimateEffect(effectBrush);
        }