Skip to content

Instantly share code, notes, and snippets.

@rootasjey
Created October 2, 2016 12:33
Show Gist options
  • Save rootasjey/ca502f073a7138c9a4a850fd8eb20ae4 to your computer and use it in GitHub Desktop.
Save rootasjey/ca502f073a7138c9a4a850fd8eb20ae4 to your computer and use it in GitHub Desktop.
Add and apply a GaussianBlur effect on a image
// Install win2D (nuget package)
// ParallaxImage is an Image Control in my Page.xaml
// ParallaxCanvas is a Canvas Control on my Page.xaml
// ParallaxCanvas contains ParallaxImage, so ParallaxCanvas is the parent of ParallaxImage
Visual _backgroundVisual = ElementCompositionPreview.GetElementVisual(ParallaxImage);
Compositor _backgroundCompositor = _backgroundVisual.Compositor;
GaussianBlurEffect blurEffect = new GaussianBlurEffect() {
Name = "Blur",
BlurAmount = 90.0f, // You can place your blur amount here.
BorderMode = EffectBorderMode.Hard,
Optimization = EffectOptimization.Balanced,
Source = new CompositionEffectSourceParameter("Backdrop")
};
var effectFactory = _backgroundCompositor.CreateEffectFactory(blurEffect, new[] { "Blur.BlurAmount" });
var effectBrush = effectFactory.CreateBrush();
var destinationBrush = _backgroundCompositor.CreateBackdropBrush();
effectBrush.SetSourceParameter("Backdrop", destinationBrush);
var blurSprite = _backgroundCompositor.CreateSpriteVisual();
blurSprite.Size = new Vector2((float)ParallaxCanvas.ActualWidth, (float)ParallaxCanvas.ActualHeight);
blurSprite.Brush = effectBrush;
ElementCompositionPreview.SetElementChildVisual(ParallaxCanvas, blurSprite);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment