private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Initialize Composition UI infrastructure.
            _root         = Container.GetVisual();
            _compositor   = _root.Compositor;
            _imageFactory = CompositionImageFactory.CreateCompositionImageFactory(_compositor);

            // Hook the sprite visual into the XAML visual tree.
            _spriteVisual = _compositor.CreateSpriteVisual();
            var side = (float)Math.Min(Presenter.ActualWidth, Presenter.ActualHeight);

            _spriteVisual.Size = new Vector2(side, side);
            _root.Children.InsertAtTop(_spriteVisual);

            // Create the effect, but don't specify the Angle yet.
            var hueRotationEffect = new HueRotationEffect
            {
                Name   = "hueRotation",
                Source = new CompositionEffectSourceParameter("source")
            };

            // Compile the effect
            var effectFactory = _compositor.CreateEffectFactory(hueRotationEffect, new[] { "hueRotation.Angle" });

            // Create and apply the brush.
            _brush = effectFactory.CreateBrush();
            _spriteVisual.Brush = _brush;

            ColorWheelButton.IsChecked = true;
        }
        void CreateBrushes(CanvasAnimatedControl sender, CanvasBitmap bitmapTiger)
        {
            var bitmapSize = bitmapTiger.Size;
            var scale = (radius * 2) / (float)bitmapSize.Height;

            var backgroundEffect = new Transform2DEffect()
            {
                Source = bitmapTiger,
                TransformMatrix = Matrix3x2.CreateScale(scale, scale) * Matrix3x2.CreateTranslation(center - radius, center - radius)
            };

            backgroundBrush = new CanvasImageBrush(sender, backgroundEffect)
            {
                SourceRectangle = new Rect(0, 0, size, size),
                Opacity = 0.6f
            };

            hueRotationEffect = new HueRotationEffect()
            {
                Source = backgroundEffect,
                Angle = (float)Math.PI * 0.5f
            };

            var foregroundEffect = new GaussianBlurEffect()
            {
                Source = hueRotationEffect,
                BlurAmount = 10
            };

            foregroundBrush = new CanvasImageBrush(sender, foregroundEffect)
            {
                SourceRectangle = new Rect(0, 0, size, size)
            };
        }
Exemple #3
0
        async Task CreateResourcesAsync(CanvasControl sender)
        {
            CanvasBitmap = await CanvasBitmap.LoadAsync(sender, "Flower1.jpg");

            canvasEffect        = new HueRotationEffect();
            canvasEffect.Source = CanvasBitmap;
            canvasEffect.Angle  = 250;
        }
Exemple #4
0
        private ICanvasImage ApplyFilterTemplate(ICanvasImage source)
        {
            if (_filter_index == 0)  //无滤镜
            {
                return(source);
            }
            else if (_filter_index == 3)  // 黑白
            {
                return(new GrayscaleEffect
                {
                    Source = source
                });
            }
            else if (_filter_index == 1)  //反色
            {
                return(new InvertEffect
                {
                    Source = source
                });
            }
            else if (_filter_index == 2) //冷淡
            {
                var hueRotationEffect = new HueRotationEffect
                {
                    Source = source,
                    Angle  = 0.5f
                };
                return(hueRotationEffect);
            }
            else if (_filter_index == 4)  //美食
            {
                var temperatureAndTintEffect = new TemperatureAndTintEffect
                {
                    Source = source
                };
                temperatureAndTintEffect.Temperature = 0.6f;
                temperatureAndTintEffect.Tint        = 0.6f;

                return(temperatureAndTintEffect);
            }

            else if (_filter_index == 5) //雕刻
            {
                var embossEffect = new EmbossEffect
                {
                    Source = source
                };
                embossEffect.Amount = 5;
                embossEffect.Angle  = 0;
                return(embossEffect);
            }

            else
            {
                return(source);
            }
        }
        public CanvasRenderTarget applyHueRotationEffects(CanvasBitmap workingBitmap)
        {
            //CanvasBitmap workingBitmap = SelectWorkingBitmap(useOriginalBitmap);

            if (workingBitmap != null)
            {
                int ww = (int)workingBitmap.SizeInPixels.Width;
                int hh = (int)workingBitmap.SizeInPixels.Height;

                TemperatureAndTintEffect temperatureAndTintEffect = new TemperatureAndTintEffect();
                temperatureAndTintEffect.Source      = workingBitmap;
                temperatureAndTintEffect.Temperature = (float)hueTemperature;
                temperatureAndTintEffect.Tint        = (float)hueTint;

                HueRotationEffect hueRotationEffect = new HueRotationEffect();
                hueRotationEffect.Angle  = (float)hueRotationAngle;
                hueRotationEffect.Source = temperatureAndTintEffect;

                PosterizeEffect     posterizeEffect     = null;
                EdgeDetectionEffect edgeDetectionEffect = null;
                if (hueDoPosterize)
                {
                    posterizeEffect                 = new PosterizeEffect();
                    posterizeEffect.Source          = hueRotationEffect;
                    posterizeEffect.RedValueCount   = huePosterizeRedCount;
                    posterizeEffect.BlueValueCount  = huePosterizeBlueCount;
                    posterizeEffect.GreenValueCount = huePosterizeGreenCount;

                    edgeDetectionEffect              = new EdgeDetectionEffect();
                    edgeDetectionEffect.Source       = posterizeEffect;
                    edgeDetectionEffect.Amount       = (float)0.9;
                    edgeDetectionEffect.BlurAmount   = 1;
                    edgeDetectionEffect.OverlayEdges = true;
                }

                //if (canvasRenderTarget != null)
                //    canvasRenderTarget.Dispose();
                CanvasRenderTarget canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), ww, hh, canvasBitmap.Dpi);
                using (var session = canvasRenderTarget.CreateDrawingSession())
                {
                    if (hueDoPosterize)
                    {
                        //session.DrawImage(posterizeEffect);
                        session.DrawImage(edgeDetectionEffect);
                    }
                    else
                    {
                        session.DrawImage(hueRotationEffect);
                    }
                }

                return(canvasRenderTarget);
            }

            return(null);
        }
 public DestRectDemo(DrawImageEmulations example, CanvasControl sender)
 {
     fillPattern  = example.checkedFillPattern;
     sourceBitmap = example.tiger;
     sourceEffect = new HueRotationEffect()
     {
         Source = sourceBitmap,
         Angle  = 1
     };
 }
        /// <summary>
        /// Creates a CompositionEffectFactory that creates HueRotationEffects.
        /// </summary>
        /// <returns>CompositionEffectFactory</returns>
        private CompositionEffectFactory CreateHueRotationEffectFactory()
        {
            var effectDefinition = new HueRotationEffect()
            {
                Name   = "HueRotation",
                Angle  = 0.0f,
                Source = new CompositionEffectSourceParameter("Video")
            };

            return(_compositor.CreateEffectFactory(effectDefinition, new string[] { "HueRotation.Angle" }));
        }
        async Task Canvas_CreateResourcesAsync(CanvasControl sender)
        {
            bitmap = await CanvasBitmap.LoadAsync(sender, "imageTiger.jpg");

            redBrush   = CreateGradientBrush(sender, 255, 0, 0);
            greenBrush = CreateGradientBrush(sender, 0, 255, 0);
            blueBrush  = CreateGradientBrush(sender, 0, 0, 255);

            brightnessEffect = new BrightnessEffect  { Source = bitmap };
            saturationEffect = new SaturationEffect  { Source = brightnessEffect };
            hueEffect        = new HueRotationEffect { Source = saturationEffect };
        }
Exemple #9
0
        private ICanvasImage CreateHueRotation()
        {
            var hueRotationEffect = new HueRotationEffect
            {
                Source = bitmapTiger
            };

            // Animation changes the hue.
            animationFunction = elapsedTime =>
            {
                hueRotationEffect.Angle = elapsedTime * 4;
            };

            return(hueRotationEffect);
        }
Exemple #10
0
        async Task Canvas_CreateResourcesAsync(CanvasControl sender)
        {
            bitmap = await CanvasBitmap.LoadAsync(sender, "imageTiger.jpg");

            redBrush   = CreateGradientBrush(sender, 255, 0, 0);
            greenBrush = CreateGradientBrush(sender, 0, 255, 0);
            blueBrush  = CreateGradientBrush(sender, 0, 0, 255);

            brightnessEffect = new BrightnessEffect {
                Source = bitmap
            };
            saturationEffect = new SaturationEffect {
                Source = brightnessEffect
            };
            hueEffect = new HueRotationEffect {
                Source = saturationEffect
            };
        }
Exemple #11
0
        public CompositionEffectBrush CreateEffectBrush(
            EffectType effectType,
            string effectName,
            float propertyValue,
            IEnumerable <string> properties)
        {
            IGraphicsEffect effectDesc = null;

            switch (effectType)
            {
            case EffectType.Saturation:
                effectDesc = new SaturationEffect()
                {
                    Name       = effectName,
                    Saturation = propertyValue,
                    Source     = new CompositionEffectSourceParameter("source")
                };
                break;

            case EffectType.HueRotation:
                effectDesc = new HueRotationEffect()
                {
                    Name   = effectName,
                    Angle  = propertyValue,
                    Source = new CompositionEffectSourceParameter("source")
                };
                break;

            case EffectType.Sepia:
                effectDesc = new SepiaEffect()
                {
                    Name      = effectName,
                    Intensity = propertyValue,
                    Source    = new CompositionEffectSourceParameter("source")
                };
                break;
            }

            CompositionEffectFactory effectFactory = _compositor.CreateEffectFactory(effectDesc, properties);
            CompositionEffectBrush   effectBrush   = effectFactory.CreateBrush();

            return(effectBrush);
        }
            public OffsetDemo(DrawImageEmulations example, CanvasControl sender)
            {
                fillPattern = example.checkedFillPattern;

                var rt = new CanvasRenderTarget(sender, (float)example.tiger.Size.Width, (float)example.tiger.Size.Height, sender.Dpi / 3);

                using (var ds = rt.CreateDrawingSession())
                {
                    ds.DrawImage(example.tiger, rt.Bounds);
                }
                sourceBitmap = rt;

                sourceEffect = new HueRotationEffect()
                {
                    Source = sourceBitmap,
                    Angle  = 1
                };

                showSourceRectRT = new CanvasRenderTarget(sender, (float)rt.Size.Width, (float)rt.Size.Height, rt.Dpi);
            }
Exemple #13
0
        private void EnsureComposition()
        {
            if (_compositor == null)
            {
                // Get our Compositor and create a SpriteVisual.
                _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
                _visual     = _compositor.CreateSpriteVisual();

                // Set the size to the size of the page, and make sure to update it whenever
                // the page's size changes.
                _visual.Size = new Vector2((float)ActualWidth, (float)ActualHeight);
                SizeChanged += (s, a) =>
                {
                    _visual.Size = new Vector2((float)ActualWidth, (float)ActualHeight);
                };

                // Attach our visual to the tree.
                ElementCompositionPreview.SetElementChildVisual(this, _visual);

                // Create our HueRotation effect.
                var graphicsEffect = new HueRotationEffect
                {
                    Name   = "HueRotation",
                    Angle  = 0.0f,
                    Source = new CompositionEffectSourceParameter("Video")
                };

                var effectFactory = _compositor.CreateEffectFactory(
                    graphicsEffect,
                    new string[] { "HueRotation.Angle" });

                _effectBrush = effectFactory.CreateBrush();

                // Apply our effect brush to our visual.
                _visual.Brush = _effectBrush;
            }
        }
Exemple #14
0
        private async void btnUpload_Clicked(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");

            // Pick only one file once a time
            StorageFile file = await openPicker.PickSingleFileAsync();

            if (file == null)
            {
                return;
            }

            using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
            {
                cl = new CanvasCommandList(canvas);

                using (CanvasDrawingSession clds = cl.CreateDrawingSession())
                {
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

                    clds.DrawImage(
                        CanvasBitmap.CreateFromSoftwareBitmap(canvas,
                                                              await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Rgba16, BitmapAlphaMode.Premultiplied)));
                }

                blur = new GaussianBlurEffect
                {
                    Source     = cl,
                    BlurAmount = (float)gaussianBlurAmountSlider.Value
                };

                hueRotation = new HueRotationEffect
                {
                    Source = blur,
                    Angle  = (float)hueRotationAmountSlider.Value / 100f * 360f
                };

                contrast = new ContrastEffect
                {
                    Source   = hueRotation,
                    Contrast = (float)(contrastAmountSlider.Value - 50f) / 50f
                };

                saturation = new SaturationEffect
                {
                    Source     = contrast,
                    Saturation = (float)saturationAmountSlider.Value / 100f
                };

                temperatureAndTint = new TemperatureAndTintEffect
                {
                    Source      = saturation,
                    Temperature = (float)(temperatureAmountSlider.Value - 50f) / 50f,
                    Tint        = (float)(tintAmountSlider.Value - 50f) / 50f
                };

                grayscale = new GrayscaleEffect
                {
                    Source = temperatureAndTint
                };

                canvasEffect = saturation;

                // CanvasControl.Invalidate Method iIndicates that the contents of the CanvasControl need to be redrawn.
                // Calling Invalidate results in the Draw event being raised shortly afterward.
                //
                // Reference: https://microsoft.github.io/Win2D/html/M_Microsoft_Graphics_Canvas_UI_Xaml_CanvasControl_Invalidate.htm
                canvas.Invalidate();

                gaussianBlurAmountSlider.IsEnabled = true;
                hueRotationAmountSlider.IsEnabled  = true;
                contrastAmountSlider.IsEnabled     = true;
                saturationAmountSlider.IsEnabled   = true;
                temperatureAmountSlider.IsEnabled  = true;
                tintAmountSlider.IsEnabled         = true;
                grayscaleBufferPrevision.IsEnabled = true;

                var image = new BitmapImage();
                await image.SetSourceAsync(fileStream);
            }
        }
        private async void UpdateEffect()
        {
            if (_compositor != null)
            {
                ComboBoxItem item = EffectSelection.SelectedValue as ComboBoxItem;
                IGraphicsEffect graphicsEffect = null;
                CompositionBrush secondaryBrush = null;
                string[] animatableProperties = null;

                //
                // Create the appropriate effect graph and resources
                //

                switch ((EffectTypes)item.Tag)
                {
                    case EffectTypes.Desaturation:
                        {
                            graphicsEffect = new SaturationEffect()
                            {
                                Saturation = 0.0f,
                                Source = new CompositionEffectSourceParameter("ImageSource")
                            };
                        }
                        break;

                    case EffectTypes.Hue:
                        {
                            graphicsEffect = new HueRotationEffect()
                            {
                                Name = "Hue",
                                Angle = 3.14f,
                                Source = new CompositionEffectSourceParameter("ImageSource")
                            };
                            animatableProperties = new[] { "Hue.Angle" };
                        }
                        break;

                    case EffectTypes.VividLight:
                        {
                            graphicsEffect = new BlendEffect()
                            {
                                Mode = BlendEffectMode.VividLight,
                                Foreground = new ColorSourceEffect()
                                {
                                    Name = "Base",
                                    Color = Color.FromArgb(255,80,40,40)
                                },
                                Background = new CompositionEffectSourceParameter("ImageSource"),
                            };
                            animatableProperties = new[] { "Base.Color" };
                        }
                        break;
                    case EffectTypes.Mask:
                        {
                            graphicsEffect = new CompositeEffect()
                            {
                                Mode = CanvasComposite.DestinationOver,
                                Sources =
                                {
                                    new CompositeEffect()
                                    {
                                        Mode = CanvasComposite.DestinationIn,
                                        Sources =
                                        {

                                            new CompositionEffectSourceParameter("ImageSource"),
                                            new CompositionEffectSourceParameter("SecondSource")
                                        }
                                    },
                                    new ColorSourceEffect()
                                    {
                                        Color = Color.FromArgb(200,255,255,255)
                                    },
                                }
                            };

                            CompositionDrawingSurface backgroundSurface = await SurfaceLoader.LoadFromUri(new Uri("ms-appx:///Samples/SDK Insider/ForegroundFocusEffects/mask.png"));

                            CompositionSurfaceBrush maskBrush = _compositor.CreateSurfaceBrush(backgroundSurface);
                            maskBrush.Stretch = CompositionStretch.UniformToFill;
                            maskBrush.CenterPoint = backgroundSurface.Size.ToVector2() / 2;
                            secondaryBrush = maskBrush;
                        }
                        break;
                    case EffectTypes.Blur:
                        {
                            graphicsEffect = new GaussianBlurEffect()
                            {
                                BlurAmount = 20,
                                Source = new CompositionEffectSourceParameter("ImageSource"),
                                Optimization = EffectOptimization.Balanced,
                                BorderMode = EffectBorderMode.Hard,
                            };
                        }
                        break;
                    case EffectTypes.LightenBlur:
                        {
                            graphicsEffect = new ArithmeticCompositeEffect()
                            {
                                Source1Amount = .4f,
                                Source2Amount = .6f,
                                MultiplyAmount = 0,
                                Source1 = new ColorSourceEffect()
                                {
                                    Name = "Base",
                                    Color = Color.FromArgb(255, 255, 255, 255),
                                },
                                Source2 = new GaussianBlurEffect()
                                {
                                    BlurAmount = 20,
                                    Source = new CompositionEffectSourceParameter("ImageSource"),
                                    Optimization = EffectOptimization.Balanced,
                                    BorderMode = EffectBorderMode.Hard,
                                }
                            };
                        }
                        break;
                    case EffectTypes.DarkenBlur:
                        {
                            graphicsEffect = new ArithmeticCompositeEffect()
                            {
                                Source1Amount = .4f,
                                Source2Amount = .6f,
                                MultiplyAmount = 0,
                                Source1 = new ColorSourceEffect()
                                {
                                    Name = "Base",
                                    Color = Color.FromArgb(255, 0, 0, 0),
                                },
                                Source2 = new GaussianBlurEffect()
                                {
                                    BlurAmount = 20,
                                    Source = new CompositionEffectSourceParameter("ImageSource"),
                                    Optimization = EffectOptimization.Balanced,
                                    BorderMode= EffectBorderMode.Hard,
                                }
                            };
                        }
                        break;
                    case EffectTypes.RainbowBlur:
                        {
                            graphicsEffect = new ArithmeticCompositeEffect()
                            {
                                Source1Amount = .3f,
                                Source2Amount = .7f,
                                MultiplyAmount = 0,
                                Source1 = new ColorSourceEffect()
                                {
                                    Name = "Base",
                                    Color = Color.FromArgb(255, 0, 0, 0),
                                },
                                Source2 = new GaussianBlurEffect()
                                {
                                    BlurAmount = 20,
                                    Source = new CompositionEffectSourceParameter("ImageSource"),
                                    Optimization = EffectOptimization.Balanced,
                                    BorderMode = EffectBorderMode.Hard,
                                }
                            };
                            animatableProperties = new[] { "Base.Color" };
                        }
                        break;
                    default:
                        break;
                }

                // Create the effect factory and instantiate a brush
                CompositionEffectFactory _effectFactory = _compositor.CreateEffectFactory(graphicsEffect, animatableProperties);
                CompositionEffectBrush brush = _effectFactory.CreateBrush();

                // Set the destination brush as the source of the image content
                brush.SetSourceParameter("ImageSource", _compositor.CreateBackdropBrush());

                // If his effect uses a secondary brush, set it now
                if (secondaryBrush != null)
                {
                    brush.SetSourceParameter("SecondSource", secondaryBrush);
                }

                // Update the destination layer with the fully configured brush
                _destinationSprite.Brush = brush;
            }
        }
 public DestRectDemo(DrawImageEmulations example, CanvasControl sender)
 {
     fillPattern = example.checkedFillPattern;
     sourceBitmap = example.tiger;
     sourceEffect = new HueRotationEffect()
     {
         Source = sourceBitmap,
         Angle = 1
     };
 }
            public OffsetDemo(DrawImageEmulations example, CanvasControl sender)
            {
                fillPattern = example.checkedFillPattern;

                var rt = new CanvasRenderTarget(sender, (float)example.tiger.Size.Width, (float)example.tiger.Size.Height, sender.Dpi / 3);
                using (var ds = rt.CreateDrawingSession())
                {
                    ds.DrawImage(example.tiger, rt.Bounds);
                }
                sourceBitmap = rt;

                sourceEffect = new HueRotationEffect()
                {
                    Source = sourceBitmap,
                    Angle = 1
                };

                showSourceRectRT = new CanvasRenderTarget(sender, (float)rt.Size.Width, (float)rt.Size.Height, rt.Dpi);
            }
        private void MainGridLoaded(object sender, RoutedEventArgs e)
        {
            m_compositor = ElementCompositionPreview.GetElementVisual(MainGrid).Compositor;
            m_root = m_compositor.CreateContainerVisual();
            ElementCompositionPreview.SetElementChildVisual(MainGrid, m_root);

            Size imageSize;
            m_noEffectBrush = CreateBrushFromAsset(
                "Bruno'sFamily2015 (13)-X2.jpg",
                out imageSize);
            m_imageAspectRatio = (imageSize.Width == 0 && imageSize.Height == 0) ? 1 : imageSize.Width / imageSize.Height;

            m_sprite = m_compositor.CreateSpriteVisual();
            ResizeImage(new Size(MainGrid.ActualWidth, MainGrid.ActualHeight));
            m_root.Children.InsertAtTop(m_sprite);

            // Image with alpha channel as an mask.
            var alphaMaskEffectDesc = new CompositeEffect
            {
                Mode = CanvasComposite.DestinationIn,
                Sources =
                {
                    new CompositionEffectSourceParameter("Image"),
                    new Transform2DEffect
                    {
                        Name = "MaskTransform",
                        Source = new CompositionEffectSourceParameter("Mask")
                    }
                }
            };
            m_alphaMaskEffectBrush = m_compositor.CreateEffectFactory(
                alphaMaskEffectDesc,
                new[] { "MaskTransform.TransformMatrix" }
            ).CreateBrush();
            m_alphaMaskEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);
            m_alphaMaskEffectBrush.SetSourceParameter(
                "Mask",
                CreateBrushFromAsset("CircleMask.png"));

            // Arithmetic operations between two images.
            var arithmeticEffectDesc = new ArithmeticCompositeEffect
            {
                Name = "effect",
                ClampOutput = false,
                Source1 = new CompositionEffectSourceParameter("Source1"),
                Source2 = new CompositionEffectSourceParameter("Source2")
            };
            m_arithmeticEffectBrush = m_compositor.CreateEffectFactory(
                arithmeticEffectDesc,
                new[]
                {
                    "effect.MultiplyAmount",
                    "effect.Source1Amount",
                    "effect.Source2Amount",
                    "effect.Offset"
                }
            ).CreateBrush();
            m_arithmeticEffectBrush.SetSourceParameter(
                "Source1",
                m_noEffectBrush);
            m_arithmeticEffectBrush.SetSourceParameter(
                "Source2",
                CreateBrushFromAsset("_P2A8041.jpg"));

            // Creates a blend effect that combines two images.
            var foregroundBrush = CreateBrushFromAsset("Checkerboard_100x100.png");
            m_blendEffectBrushes = new CompositionEffectBrush[m_supportedBlendModes.Length];
            for (int i = 0; i < m_supportedBlendModes.Length; i++)
            {
                var blendEffectDesc = new BlendEffect
                {
                    Mode = m_supportedBlendModes[i],
                    Background = new CompositionEffectSourceParameter("Background"),
                    Foreground = new CompositionEffectSourceParameter("Foreground")
                };
                m_blendEffectBrushes[i] = m_compositor.CreateEffectFactory(
                    blendEffectDesc
                ).CreateBrush();
                m_blendEffectBrushes[i].SetSourceParameter(
                    "Background",
                    m_noEffectBrush);
                m_blendEffectBrushes[i].SetSourceParameter(
                    "Foreground",
                    foregroundBrush);
            }

            // Generates an image containing a solid color.
            var colorSourceEffectDesc = new ColorSourceEffect // FloodEffect
            {
                Name = "effect"
            };
            m_colorSourceEffectBrush = m_compositor.CreateEffectFactory(
                colorSourceEffectDesc,
                new[] { "effect.Color" }
            ).CreateBrush();

            // Changes the contrast of an image.
            var contrastEffectDesc = new ContrastEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_contrastEffectBrush = m_compositor.CreateEffectFactory(
                contrastEffectDesc,
                new[] { "effect.Contrast" }
            ).CreateBrush();
            m_contrastEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Changes the exposure of an image.
            var exposureEffectDesc = new ExposureEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_exposureEffectBrush = m_compositor.CreateEffectFactory(
                exposureEffectDesc,
                new[] { "effect.Exposure" }
            ).CreateBrush();
            m_exposureEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the colors of an image by applying a per-channel gamma transfer function.
            var gammaTransferEffectDesc = new GammaTransferEffect
            {
                Name = "effect",
                RedDisable = false,
                GreenDisable = false,
                BlueDisable = false,
                AlphaDisable = false,
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_gammaTransferEffectBrush = m_compositor.CreateEffectFactory(
                gammaTransferEffectDesc,
                new[]
                {
                    "effect.RedAmplitude",
                    "effect.RedExponent",
                    "effect.RedOffset",
                    "effect.GreenAmplitude",
                    "effect.GreenExponent",
                    "effect.GreenOffset",
                    "effect.BlueAmplitude",
                    "effect.BlueExponent",
                    "effect.BlueOffset"
                }
            ).CreateBrush();
            m_gammaTransferEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Converts an image to monochromatic gray.
            var grayscaleEffectDesc = new GrayscaleEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_grayscaleEffectBrush = m_compositor.CreateEffectFactory(
                grayscaleEffectDesc
            ).CreateBrush();
            m_grayscaleEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the color of an image by rotating its hue values.
            var hueRotationEffectDesc = new HueRotationEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_hueRotationEffectBrush = m_compositor.CreateEffectFactory(
                hueRotationEffectDesc,
                new[] { "effect.Angle" }
            ).CreateBrush();
            m_hueRotationEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Inverts the colors of an image.
            var invertEffectDesc = new InvertEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_invertEffectBrush = m_compositor.CreateEffectFactory(
                invertEffectDesc
            ).CreateBrush();
            m_invertEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the saturation of an image.
            var saturationEffectDesc = new SaturationEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_saturateEffectBrush = m_compositor.CreateEffectFactory(
                saturationEffectDesc,
                new[] { "effect.Saturation" }
            ).CreateBrush();
            m_saturateEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Converts an image to sepia tones.
            var supportedAlphaModes = new[]
            {
                CanvasAlphaMode.Premultiplied,
                CanvasAlphaMode.Straight
            };
            m_sepiaEffectBrushes = new CompositionEffectBrush[supportedAlphaModes.Length];
            for (int i = 0; i < supportedAlphaModes.Length; i++)
            {
                var sepiaEffectDesc = new SepiaEffect
                {
                    Name = "effect",
                    AlphaMode = supportedAlphaModes[i],
                    Source = new CompositionEffectSourceParameter("Image")
                };
                m_sepiaEffectBrushes[i] = m_compositor.CreateEffectFactory(
                    sepiaEffectDesc,
                    new[] { "effect.Intensity" }
                ).CreateBrush();
                m_sepiaEffectBrushes[i].SetSourceParameter(
                    "Image",
                    m_noEffectBrush);
            }

            // Adjusts the temperature and/or tint of an image.
            var temperatureAndTintEffectDesc = new TemperatureAndTintEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_temperatureAndTintEffectBrush = m_compositor.CreateEffectFactory(
                temperatureAndTintEffectDesc,
                new[]
                {
                    "effect.Temperature",
                    "effect.Tint"
                }
            ).CreateBrush();
            m_temperatureAndTintEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Applies a 2D affine transform matrix to an image.
            var transform2DEffectDesc = new Transform2DEffect
            {
                TransformMatrix = new Matrix3x2(
                    -1, 0,
                    0, 1,
                    m_sprite.Size.X, 0),
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_transform2DEffectBrush = m_compositor.CreateEffectFactory(
                transform2DEffectDesc
            ).CreateBrush();
            m_transform2DEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // For simplying UI states switch, put effect parameter grids in an array
            m_effectParamsGrids = new Grid[(int)EffectType.NumEffectTypes];
            m_effectParamsGrids[(int)EffectType.NoEffect] = null;
            m_effectParamsGrids[(int)EffectType.AlphaMask] = AlphaMaskParams;
            m_effectParamsGrids[(int)EffectType.Arithmetic] = ArithmeticParams;
            m_effectParamsGrids[(int)EffectType.Blend] = BlendParams;
            m_effectParamsGrids[(int)EffectType.ColorSource] = ColorSourceParams;
            m_effectParamsGrids[(int)EffectType.Contrast] = ContrastParams;
            m_effectParamsGrids[(int)EffectType.Exposure] = ExposureParams;
            m_effectParamsGrids[(int)EffectType.GammaTransfer] = GammaTransferParams;
            m_effectParamsGrids[(int)EffectType.Grayscale] = null;
            m_effectParamsGrids[(int)EffectType.HueRotation] = HueRotationParams;
            m_effectParamsGrids[(int)EffectType.Invert] = null;
            m_effectParamsGrids[(int)EffectType.Saturation] = SaturationParams;
            m_effectParamsGrids[(int)EffectType.Sepia] = SepiaParams;
            m_effectParamsGrids[(int)EffectType.TemperatureAndTint] = TemperatureAndTintParams;
            m_effectParamsGrids[(int)EffectType.Transform2D] = null;

            // Same as grids
            m_effectBrushes = new CompositionBrush[(int)EffectType.NumEffectTypes];
            m_effectBrushes[(int)EffectType.NoEffect] = m_noEffectBrush;
            m_effectBrushes[(int)EffectType.AlphaMask] = m_alphaMaskEffectBrush;
            m_effectBrushes[(int)EffectType.Arithmetic] = m_arithmeticEffectBrush;
            m_effectBrushes[(int)EffectType.Blend] = m_blendEffectBrushes[m_activeBlendMode];
            m_effectBrushes[(int)EffectType.ColorSource] = m_colorSourceEffectBrush;
            m_effectBrushes[(int)EffectType.Contrast] = m_contrastEffectBrush;
            m_effectBrushes[(int)EffectType.Exposure] = m_exposureEffectBrush;
            m_effectBrushes[(int)EffectType.GammaTransfer] = m_gammaTransferEffectBrush;
            m_effectBrushes[(int)EffectType.Grayscale] = m_grayscaleEffectBrush;
            m_effectBrushes[(int)EffectType.HueRotation] = m_hueRotationEffectBrush;
            m_effectBrushes[(int)EffectType.Invert] = m_invertEffectBrush;
            m_effectBrushes[(int)EffectType.Saturation] = m_saturateEffectBrush;
            m_effectBrushes[(int)EffectType.Sepia] = m_sepiaEffectBrushes[m_activeSepiaAlphaMode];
            m_effectBrushes[(int)EffectType.TemperatureAndTint] = m_temperatureAndTintEffectBrush;
            m_effectBrushes[(int)EffectType.Transform2D] = m_transform2DEffectBrush;

            this.InitializeValues();
        }
Exemple #19
0
        private ICanvasImage ApplyFilter(ICanvasImage source)
        {
            if (filterIndex == 0) // NONE
            {
                return(source);
            }
            else if (filterIndex == 1)
            {
                return(new GrayscaleEffect
                {
                    Source = source
                });
            }
            else if (filterIndex == 2)
            {
                return(new InvertEffect
                {
                    Source = source
                });
            }
            else if (filterIndex == 3)
            {
                var hueRotationEffect = new HueRotationEffect
                {
                    Source = source,
                    Angle  = 0.5f
                };
                return(hueRotationEffect);
            }
            else if (filterIndex == 4)
            {
                var temperatureAndTintEffect = new TemperatureAndTintEffect
                {
                    Source = source
                };
                temperatureAndTintEffect.Temperature = 0.6f;
                temperatureAndTintEffect.Tint        = 0.6f;

                return(temperatureAndTintEffect);
            }
            else if (filterIndex == 5)
            {
                var temperatureAndTintEffect = new TemperatureAndTintEffect
                {
                    Source = source
                };
                temperatureAndTintEffect.Temperature = -0.6f;
                temperatureAndTintEffect.Tint        = -0.6f;

                return(temperatureAndTintEffect);
            }
            else if (filterIndex == 6)
            {
                var vignetteEffect = new VignetteEffect
                {
                    Source = source
                };
                vignetteEffect.Color  = Color.FromArgb(255, 0xFF, 0xFF, 0xFF);
                vignetteEffect.Amount = 0.6f;

                return(vignetteEffect);
            }
            else if (filterIndex == 7)
            {
                var embossEffect = new EmbossEffect
                {
                    Source = source
                };
                embossEffect.Amount = 5;
                embossEffect.Angle  = 0;
                return(embossEffect);
            }
            else if (filterIndex == 8)
            {
                var sepiaEffect = new SepiaEffect
                {
                    Source = source
                };
                sepiaEffect.Intensity = 1;
                return(sepiaEffect);
            }
            else // NONE
            {
                return(source);
            }
        }
Exemple #20
0
        private ICanvasImage ApplyFilterTemplate(ICanvasImage source)
        {
            if (_filter_index == 0)  //无滤镜
            {
                return(source);
            }
            else if (_filter_index == 1)  // 黑白
            {
                return(new GrayscaleEffect
                {
                    Source = source
                });
            }
            else if (_filter_index == 2)  //反色
            {
                return(new InvertEffect
                {
                    Source = source
                });
            }
            else if (_filter_index == 3) //冷色
            {
                var hueRotationEffect = new HueRotationEffect
                {
                    Source = source,
                    Angle  = 0.5f
                };
                return(hueRotationEffect);
            }
            else if (_filter_index == 4)  //美食
            {
                var temperatureAndTintEffect = new TemperatureAndTintEffect
                {
                    Source = source
                };
                temperatureAndTintEffect.Temperature = 0.6f;
                temperatureAndTintEffect.Tint        = 0.6f;

                return(temperatureAndTintEffect);
            }
            else if (_filter_index == 5) //冷绿
            {
                var temperatureAndTintEffect = new TemperatureAndTintEffect
                {
                    Source = source
                };
                temperatureAndTintEffect.Temperature = -0.6f;
                temperatureAndTintEffect.Tint        = -0.6f;

                return(temperatureAndTintEffect);
            }
            else if (_filter_index == 6) //梦幻
            {
                var vignetteEffect = new VignetteEffect
                {
                    Source = source
                };
                vignetteEffect.Color  = Color.FromArgb(255, 0xFF, 0xFF, 0xFF);
                vignetteEffect.Amount = 0.6f;

                return(vignetteEffect);
            }
            else if (_filter_index == 7) //浮雕
            {
                var embossEffect = new EmbossEffect
                {
                    Source = source
                };
                embossEffect.Amount = 5;
                embossEffect.Angle  = 0;
                return(embossEffect);
            }
            else if (_filter_index == 8) //怀旧
            {
                var sepiaEffect = new SepiaEffect
                {
                    Source = source
                };
                sepiaEffect.Intensity = 1;
                return(sepiaEffect);
            }
            else if (_filter_index == 9)//运动
            {
                var directEffect = new DirectionalBlurEffect
                {
                    Source = source
                };
                directEffect.BlurAmount = 12;
                directEffect.BorderMode = EffectBorderMode.Soft;
                directEffect.Angle      = 3.14F;
                return(directEffect);
            }
            else
            {
                return(source);
            }
        }
Exemple #21
0
        private async void UpdateEffect()
        {
            if (_compositor != null)
            {
                ComboBoxItem     item                 = EffectSelection.SelectedValue as ComboBoxItem;
                IGraphicsEffect  graphicsEffect       = null;
                CompositionBrush secondaryBrush       = null;
                string[]         animatableProperties = null;

                //
                // Create the appropriate effect graph and resources
                //

                switch ((EffectTypes)item.Tag)
                {
                case EffectTypes.Desaturation:
                {
                    graphicsEffect = new SaturationEffect()
                    {
                        Saturation = 0.0f,
                        Source     = new CompositionEffectSourceParameter("ImageSource")
                    };
                }
                break;

                case EffectTypes.Hue:
                {
                    graphicsEffect = new HueRotationEffect()
                    {
                        Name   = "Hue",
                        Angle  = 3.14f,
                        Source = new CompositionEffectSourceParameter("ImageSource")
                    };
                    animatableProperties = new[] { "Hue.Angle" };
                }
                break;

                case EffectTypes.VividLight:
                {
                    graphicsEffect = new BlendEffect()
                    {
                        Mode       = BlendEffectMode.VividLight,
                        Foreground = new ColorSourceEffect()
                        {
                            Name  = "Base",
                            Color = Color.FromArgb(255, 80, 40, 40)
                        },
                        Background = new CompositionEffectSourceParameter("ImageSource"),
                    };
                    animatableProperties = new[] { "Base.Color" };
                }
                break;

                case EffectTypes.Mask:
                {
                    graphicsEffect = new CompositeEffect()
                    {
                        Mode    = CanvasComposite.DestinationOver,
                        Sources =
                        {
                            new CompositeEffect()
                            {
                                Mode    = CanvasComposite.DestinationIn,
                                Sources =
                                {
                                    new CompositionEffectSourceParameter("ImageSource"),
                                    new CompositionEffectSourceParameter("SecondSource")
                                }
                            },
                            new ColorSourceEffect()
                            {
                                Color = Color.FromArgb(200, 255, 255, 255)
                            },
                        }
                    };

                    CompositionDrawingSurface backgroundSurface = await SurfaceLoader.LoadFromUri(new Uri("ms-appx:///Samples/SDK 14393/ForegroundFocusEffects/mask.png"));

                    CompositionSurfaceBrush maskBrush = _compositor.CreateSurfaceBrush(backgroundSurface);
                    maskBrush.Stretch     = CompositionStretch.UniformToFill;
                    maskBrush.CenterPoint = _destinationSprite.Size * .5f;
                    secondaryBrush        = maskBrush;
                }
                break;

                case EffectTypes.Blur:
                {
                    graphicsEffect = new GaussianBlurEffect()
                    {
                        BlurAmount   = 20,
                        Source       = new CompositionEffectSourceParameter("ImageSource"),
                        Optimization = EffectOptimization.Balanced,
                        BorderMode   = EffectBorderMode.Hard,
                    };
                }
                break;

                case EffectTypes.LightenBlur:
                {
                    graphicsEffect = new ArithmeticCompositeEffect()
                    {
                        Source1Amount  = .4f,
                        Source2Amount  = .6f,
                        MultiplyAmount = 0,
                        Source1        = new ColorSourceEffect()
                        {
                            Name  = "Base",
                            Color = Color.FromArgb(255, 255, 255, 255),
                        },
                        Source2 = new GaussianBlurEffect()
                        {
                            BlurAmount   = 20,
                            Source       = new CompositionEffectSourceParameter("ImageSource"),
                            Optimization = EffectOptimization.Balanced,
                            BorderMode   = EffectBorderMode.Hard,
                        }
                    };
                }
                break;

                case EffectTypes.DarkenBlur:
                {
                    graphicsEffect = new ArithmeticCompositeEffect()
                    {
                        Source1Amount  = .4f,
                        Source2Amount  = .6f,
                        MultiplyAmount = 0,
                        Source1        = new ColorSourceEffect()
                        {
                            Name  = "Base",
                            Color = Color.FromArgb(255, 0, 0, 0),
                        },
                        Source2 = new GaussianBlurEffect()
                        {
                            BlurAmount   = 20,
                            Source       = new CompositionEffectSourceParameter("ImageSource"),
                            Optimization = EffectOptimization.Balanced,
                            BorderMode   = EffectBorderMode.Hard,
                        }
                    };
                }
                break;

                case EffectTypes.RainbowBlur:
                {
                    graphicsEffect = new ArithmeticCompositeEffect()
                    {
                        Source1Amount  = .3f,
                        Source2Amount  = .7f,
                        MultiplyAmount = 0,
                        Source1        = new ColorSourceEffect()
                        {
                            Name  = "Base",
                            Color = Color.FromArgb(255, 0, 0, 0),
                        },
                        Source2 = new GaussianBlurEffect()
                        {
                            BlurAmount   = 20,
                            Source       = new CompositionEffectSourceParameter("ImageSource"),
                            Optimization = EffectOptimization.Balanced,
                            BorderMode   = EffectBorderMode.Hard,
                        }
                    };
                    animatableProperties = new[] { "Base.Color" };
                }
                break;

                default:
                    break;
                }

                // Create the effect factory and instantiate a brush
                CompositionEffectFactory _effectFactory = _compositor.CreateEffectFactory(graphicsEffect, animatableProperties);
                CompositionEffectBrush   brush          = _effectFactory.CreateBrush();

                // Set the destination brush as the source of the image content
                brush.SetSourceParameter("ImageSource", _compositor.CreateBackdropBrush());

                // If his effect uses a secondary brush, set it now
                if (secondaryBrush != null)
                {
                    brush.SetSourceParameter("SecondSource", secondaryBrush);
                }

                // Update the destination layer with the fully configured brush
                _destinationSprite.Brush = brush;
            }
        }
        public CompositionEffectBrush CreateEffectBrush(
            EffectType effectType,
            string effectName,
            float propertyValue,
            IEnumerable<string> properties)
        {
            IGraphicsEffect effectDesc = null;

            switch (effectType)
            {
                case EffectType.Saturation:
                    effectDesc = new SaturationEffect()
                    {
                        Name = effectName,
                        Saturation = propertyValue,
                        Source = new CompositionEffectSourceParameter("source")
                    };
                    break;

                case EffectType.HueRotation:
                    effectDesc = new HueRotationEffect()
                    {
                        Name = effectName,
                        Angle = propertyValue,
                        Source = new CompositionEffectSourceParameter("source")
                    };
                    break;

                case EffectType.Sepia:
                    effectDesc = new SepiaEffect()
                    {
                        Name = effectName,
                        Intensity = propertyValue,
                        Source = new CompositionEffectSourceParameter("source")
                    };
                    break;
            }

            CompositionEffectFactory effectFactory = _compositor.CreateEffectFactory(effectDesc, properties);
            CompositionEffectBrush effectBrush = effectFactory.CreateBrush();

            return effectBrush;
        }
        void CreateBrushes(CanvasAnimatedControl sender, CanvasBitmap bitmapTiger)
        {
            var bitmapSize = bitmapTiger.Size;
            var scale = (radius * 2) / (float)bitmapSize.Height;

            var backgroundEffect = new Transform2DEffect()
            {
                Source = bitmapTiger,
                TransformMatrix = Matrix3x2.CreateScale(scale, scale) * Matrix3x2.CreateTranslation(center - radius, center - radius)
            };

            backgroundBrush = new CanvasImageBrush(sender, backgroundEffect)
            {
                SourceRectangle = new Rect(0, 0, size, size),
                Opacity = 0.6f
            };

            hueRotationEffect = new HueRotationEffect()
            {
                Source = backgroundEffect,
                Angle = (float)Math.PI * 0.5f
            };

            var foregroundEffect = new GaussianBlurEffect()
            {
                Source = hueRotationEffect,
                BlurAmount = 10
            };

            foregroundBrush = new CanvasImageBrush(sender, foregroundEffect)
            {
                SourceRectangle = new Rect(0, 0, size, size)
            };
        }
        private ICanvasImage CreateHueRotation()
        {
            var hueRotationEffect = new HueRotationEffect
            {
                Source = bitmapTiger
            };

            // Animation changes the hue.
            animationFunction = elapsedTime =>
            {
                hueRotationEffect.Angle = elapsedTime * 4;
            };

            return hueRotationEffect;
        }
        /// <summary>
        /// Creates a CompositionEffectFactory that creates HueRotationEffects.
        /// </summary>
        /// <returns>CompositionEffectFactory</returns>
        private CompositionEffectFactory CreateHueRotationEffectFactory()
        {
            var effectDefinition = new HueRotationEffect()
            {
                Name = "HueRotation",
                Angle = 0.0f,
                Source = new CompositionEffectSourceParameter("Video")
            };

            return _compositor.CreateEffectFactory(effectDefinition, new string[] { "HueRotation.Angle" });
        }
        private void MainGridLoaded(object sender, RoutedEventArgs e)
        {
            m_compositor = ElementCompositionPreview.GetElementVisual(MainGrid).Compositor;
            m_root       = m_compositor.CreateContainerVisual();
            ElementCompositionPreview.SetElementChildVisual(MainGrid, m_root);

            Size imageSize;

            m_noEffectBrush = CreateBrushFromAsset(
                "Bruno'sFamily2015 (13)-X2.jpg",
                out imageSize);
            m_imageAspectRatio = (imageSize.Width == 0 && imageSize.Height == 0) ? 1 : imageSize.Width / imageSize.Height;

            m_sprite = m_compositor.CreateSpriteVisual();
            ResizeImage(new Size(MainGrid.ActualWidth, MainGrid.ActualHeight));
            m_root.Children.InsertAtTop(m_sprite);

            // Image with alpha channel as an mask.
            var alphaMaskEffectDesc = new CompositeEffect
            {
                Mode    = CanvasComposite.DestinationIn,
                Sources =
                {
                    new CompositionEffectSourceParameter("Image"),
                    new Transform2DEffect
                    {
                        Name   = "MaskTransform",
                        Source = new CompositionEffectSourceParameter("Mask")
                    }
                }
            };

            m_alphaMaskEffectBrush = m_compositor.CreateEffectFactory(
                alphaMaskEffectDesc,
                new[] { "MaskTransform.TransformMatrix" }
                ).CreateBrush();
            m_alphaMaskEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);
            m_alphaMaskEffectBrush.SetSourceParameter(
                "Mask",
                CreateBrushFromAsset("CircleMask.png"));

            // Arithmetic operations between two images.
            var arithmeticEffectDesc = new ArithmeticCompositeEffect
            {
                Name        = "effect",
                ClampOutput = false,
                Source1     = new CompositionEffectSourceParameter("Source1"),
                Source2     = new CompositionEffectSourceParameter("Source2")
            };

            m_arithmeticEffectBrush = m_compositor.CreateEffectFactory(
                arithmeticEffectDesc,
                new[]
            {
                "effect.MultiplyAmount",
                "effect.Source1Amount",
                "effect.Source2Amount",
                "effect.Offset"
            }
                ).CreateBrush();
            m_arithmeticEffectBrush.SetSourceParameter(
                "Source1",
                m_noEffectBrush);
            m_arithmeticEffectBrush.SetSourceParameter(
                "Source2",
                CreateBrushFromAsset("_P2A8041.jpg"));

            // Creates a blend effect that combines two images.
            var foregroundBrush = CreateBrushFromAsset("Checkerboard_100x100.png");

            m_blendEffectBrushes = new CompositionEffectBrush[m_supportedBlendModes.Length];
            for (int i = 0; i < m_supportedBlendModes.Length; i++)
            {
                var blendEffectDesc = new BlendEffect
                {
                    Mode       = m_supportedBlendModes[i],
                    Background = new CompositionEffectSourceParameter("Background"),
                    Foreground = new CompositionEffectSourceParameter("Foreground")
                };
                m_blendEffectBrushes[i] = m_compositor.CreateEffectFactory(
                    blendEffectDesc
                    ).CreateBrush();
                m_blendEffectBrushes[i].SetSourceParameter(
                    "Background",
                    m_noEffectBrush);
                m_blendEffectBrushes[i].SetSourceParameter(
                    "Foreground",
                    foregroundBrush);
            }

            // Generates an image containing a solid color.
            var colorSourceEffectDesc = new ColorSourceEffect // FloodEffect
            {
                Name = "effect"
            };

            m_colorSourceEffectBrush = m_compositor.CreateEffectFactory(
                colorSourceEffectDesc,
                new[] { "effect.Color" }
                ).CreateBrush();

            // Changes the contrast of an image.
            var contrastEffectDesc = new ContrastEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_contrastEffectBrush = m_compositor.CreateEffectFactory(
                contrastEffectDesc,
                new[] { "effect.Contrast" }
                ).CreateBrush();
            m_contrastEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Changes the exposure of an image.
            var exposureEffectDesc = new ExposureEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_exposureEffectBrush = m_compositor.CreateEffectFactory(
                exposureEffectDesc,
                new[] { "effect.Exposure" }
                ).CreateBrush();
            m_exposureEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the colors of an image by applying a per-channel gamma transfer function.
            var gammaTransferEffectDesc = new GammaTransferEffect
            {
                Name         = "effect",
                RedDisable   = false,
                GreenDisable = false,
                BlueDisable  = false,
                AlphaDisable = false,
                Source       = new CompositionEffectSourceParameter("Image")
            };

            m_gammaTransferEffectBrush = m_compositor.CreateEffectFactory(
                gammaTransferEffectDesc,
                new[]
            {
                "effect.RedAmplitude",
                "effect.RedExponent",
                "effect.RedOffset",
                "effect.GreenAmplitude",
                "effect.GreenExponent",
                "effect.GreenOffset",
                "effect.BlueAmplitude",
                "effect.BlueExponent",
                "effect.BlueOffset"
            }
                ).CreateBrush();
            m_gammaTransferEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Converts an image to monochromatic gray.
            var grayscaleEffectDesc = new GrayscaleEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_grayscaleEffectBrush = m_compositor.CreateEffectFactory(
                grayscaleEffectDesc
                ).CreateBrush();
            m_grayscaleEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the color of an image by rotating its hue values.
            var hueRotationEffectDesc = new HueRotationEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_hueRotationEffectBrush = m_compositor.CreateEffectFactory(
                hueRotationEffectDesc,
                new[] { "effect.Angle" }
                ).CreateBrush();
            m_hueRotationEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Inverts the colors of an image.
            var invertEffectDesc = new InvertEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_invertEffectBrush = m_compositor.CreateEffectFactory(
                invertEffectDesc
                ).CreateBrush();
            m_invertEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the saturation of an image.
            var saturationEffectDesc = new SaturationEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_saturateEffectBrush = m_compositor.CreateEffectFactory(
                saturationEffectDesc,
                new[] { "effect.Saturation" }
                ).CreateBrush();
            m_saturateEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Converts an image to sepia tones.
            var supportedAlphaModes = new[]
            {
                CanvasAlphaMode.Premultiplied,
                CanvasAlphaMode.Straight
            };

            m_sepiaEffectBrushes = new CompositionEffectBrush[supportedAlphaModes.Length];
            for (int i = 0; i < supportedAlphaModes.Length; i++)
            {
                var sepiaEffectDesc = new SepiaEffect
                {
                    Name      = "effect",
                    AlphaMode = supportedAlphaModes[i],
                    Source    = new CompositionEffectSourceParameter("Image")
                };
                m_sepiaEffectBrushes[i] = m_compositor.CreateEffectFactory(
                    sepiaEffectDesc,
                    new[] { "effect.Intensity" }
                    ).CreateBrush();
                m_sepiaEffectBrushes[i].SetSourceParameter(
                    "Image",
                    m_noEffectBrush);
            }

            // Adjusts the temperature and/or tint of an image.
            var temperatureAndTintEffectDesc = new TemperatureAndTintEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_temperatureAndTintEffectBrush = m_compositor.CreateEffectFactory(
                temperatureAndTintEffectDesc,
                new[]
            {
                "effect.Temperature",
                "effect.Tint"
            }
                ).CreateBrush();
            m_temperatureAndTintEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Applies a 2D affine transform matrix to an image.
            var transform2DEffectDesc = new Transform2DEffect
            {
                TransformMatrix = new Matrix3x2(
                    -1, 0,
                    0, 1,
                    m_sprite.Size.X, 0),
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_transform2DEffectBrush = m_compositor.CreateEffectFactory(
                transform2DEffectDesc
                ).CreateBrush();
            m_transform2DEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // For simplying UI states switch, put effect parameter grids in an array
            m_effectParamsGrids = new Grid[(int)EffectType.NumEffectTypes];
            m_effectParamsGrids[(int)EffectType.NoEffect]           = null;
            m_effectParamsGrids[(int)EffectType.AlphaMask]          = AlphaMaskParams;
            m_effectParamsGrids[(int)EffectType.Arithmetic]         = ArithmeticParams;
            m_effectParamsGrids[(int)EffectType.Blend]              = BlendParams;
            m_effectParamsGrids[(int)EffectType.ColorSource]        = ColorSourceParams;
            m_effectParamsGrids[(int)EffectType.Contrast]           = ContrastParams;
            m_effectParamsGrids[(int)EffectType.Exposure]           = ExposureParams;
            m_effectParamsGrids[(int)EffectType.GammaTransfer]      = GammaTransferParams;
            m_effectParamsGrids[(int)EffectType.Grayscale]          = null;
            m_effectParamsGrids[(int)EffectType.HueRotation]        = HueRotationParams;
            m_effectParamsGrids[(int)EffectType.Invert]             = null;
            m_effectParamsGrids[(int)EffectType.Saturation]         = SaturationParams;
            m_effectParamsGrids[(int)EffectType.Sepia]              = SepiaParams;
            m_effectParamsGrids[(int)EffectType.TemperatureAndTint] = TemperatureAndTintParams;
            m_effectParamsGrids[(int)EffectType.Transform2D]        = null;

            // Same as grids
            m_effectBrushes = new CompositionBrush[(int)EffectType.NumEffectTypes];
            m_effectBrushes[(int)EffectType.NoEffect]           = m_noEffectBrush;
            m_effectBrushes[(int)EffectType.AlphaMask]          = m_alphaMaskEffectBrush;
            m_effectBrushes[(int)EffectType.Arithmetic]         = m_arithmeticEffectBrush;
            m_effectBrushes[(int)EffectType.Blend]              = m_blendEffectBrushes[m_activeBlendMode];
            m_effectBrushes[(int)EffectType.ColorSource]        = m_colorSourceEffectBrush;
            m_effectBrushes[(int)EffectType.Contrast]           = m_contrastEffectBrush;
            m_effectBrushes[(int)EffectType.Exposure]           = m_exposureEffectBrush;
            m_effectBrushes[(int)EffectType.GammaTransfer]      = m_gammaTransferEffectBrush;
            m_effectBrushes[(int)EffectType.Grayscale]          = m_grayscaleEffectBrush;
            m_effectBrushes[(int)EffectType.HueRotation]        = m_hueRotationEffectBrush;
            m_effectBrushes[(int)EffectType.Invert]             = m_invertEffectBrush;
            m_effectBrushes[(int)EffectType.Saturation]         = m_saturateEffectBrush;
            m_effectBrushes[(int)EffectType.Sepia]              = m_sepiaEffectBrushes[m_activeSepiaAlphaMode];
            m_effectBrushes[(int)EffectType.TemperatureAndTint] = m_temperatureAndTintEffectBrush;
            m_effectBrushes[(int)EffectType.Transform2D]        = m_transform2DEffectBrush;

            this.InitializeValues();
        }
        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);
        }