So, how to make such effect using C# & XAML?
-
Have you tried someting so far? Some code or anything that you've tried to do, but didn't work? – Mickey Aug 13 '15 at 13:35
-
I was looking for something like this on the Internet, but there are examples that blur the image , but do not overlap , but they are not relevant to Windows 10 , so I do not even know where to start. And to know the people help me , I wrote here , but not for you to write a response to the contempt. – SnakeAce Aug 13 '15 at 13:55
-
possible duplicate of [Blur behind transparent WPF window](http://stackoverflow.com/questions/31778017/blur-behind-transparent-wpf-window) – IInspectable Aug 18 '15 at 00:31
2 Answers
I've been trying to create something like this ever since iOS 7 introduced the frosted glass. Now thanks to Win2D, it's a lot simpler to create similar effects in WinRT.
First, you will need to get the Win2D.uwp package from NuGet.
The idea is to create a GaussianBlurEffect
based on the image source and put it on top of another white colored mask to mimic the frosted glass look.
To get the GaussianBlurEffect
ready, you will need to create a xaml control CanvasControl
from the Win2D library. So the UI structure is something like this -
<Grid x:Name="ImagePanel2" Width="356" Height="200" Margin="0,0,0,40" VerticalAlignment="Bottom">
<Image x:Name="Image2" Source="Assets/Food.jpg" Stretch="UniformToFill" />
<Grid x:Name="Overlay" ManipulationMode="TranslateX" ManipulationStarted="Overlay_ManipulationStarted" ManipulationDelta="Overlay_ManipulationDelta" ManipulationCompleted="Overlay_ManipulationCompleted" RenderTransformOrigin="0.5,0.5">
<Grid.Clip>
<RectangleGeometry x:Name="Clip" Rect="0, 0, 356, 200" />
</Grid.Clip>
<Rectangle x:Name="WhiteMask" Fill="White" />
<Xaml:CanvasControl x:Name="Canvas" CreateResources="Canvas_CreateResources" Draw="Canvas_Draw" />
</Grid>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Frosted Glass" VerticalAlignment="Top" Foreground="#FF595959" Margin="12,12,0,0" FontWeight="Light" FontSize="26.667" FontStyle="Italic" TextLineBounds="Tight" />
</Grid>
Note I've created a Clip
for the Overlay
element, 'cause I need to reduce the Rect
's third parameter (i.e. the Width) while I am panning it to the left to make an illusion that the Overlay
is sliding along with my finger.
The code behind is quite straight forward -
void Canvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}
async Task CreateResourcesAsync(CanvasControl sender)
{
// give it a little bit delay to ensure the image is load, ideally you want to Image.ImageOpened event instead
await Task.Delay(200);
using (var stream = new InMemoryRandomAccessStream())
{
// get the stream from the background image
var target = new RenderTargetBitmap();
await target.RenderAsync(this.Image2);
var pixelBuffer = await target.GetPixelsAsync();
var pixels = pixelBuffer.ToArray();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)target.PixelWidth, (uint)target.PixelHeight, 96, 96, pixels);
await encoder.FlushAsync();
stream.Seek(0);
// load the stream into our bitmap
_bitmap = await CanvasBitmap.LoadAsync(sender, stream);
}
}
void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
using (var session = args.DrawingSession)
{
var blur = new GaussianBlurEffect
{
BlurAmount = 50.0f, // increase this to make it more blurry or vise versa.
//Optimization = EffectOptimization.Balanced, // default value
//BorderMode = EffectBorderMode.Soft // default value
Source = _bitmap
};
session.DrawImage(blur, new Rect(0, 0, sender.ActualWidth, sender.ActualHeight),
new Rect(0, 0, _bitmap.SizeInPixels.Width, _bitmap.SizeInPixels.Height), 0.9f);
}
}
void Overlay_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
// reset the inital with of the Rect
_x = (float)this.ImagePanel2.ActualWidth;
}
void Overlay_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
// get the movement on X axis
_x += (float)e.Delta.Translation.X;
// keep the pan within the bountry
if (_x > this.ImagePanel2.ActualWidth || _x < 0) return;
// we clip the overlay to reveal the actual image underneath
this.Clip.Rect = new Rect(0, 0, _x, this.ImagePanel2.ActualHeight);
}
void Overlay_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
// reset the clip to show the full overlay
this.Clip.Rect = new Rect(0, 0, this.ImagePanel2.ActualWidth, this.ImagePanel2.ActualHeight);
}
You can further adjust the BlurAmount
property as well as the opacity figure (0.9f
) to get youself the exact effect you want.
Also to note that there might be another (better?) way to do this in the future. If the new Composition API does support the GaussianBlurEffect
in a future release, I will update the answer.
You can find the current working sample from this GitHub repo. And I've attached an image here to showcase how it looks like. :)

- 38,763
- 7
- 88
- 133
-
Oh, nice decision, thank you, but it's half of what I needed. So can there be any option with dynamic content? So, we have mainpage and dynamic frames there, like here: `
` Just now it works for background image. – SnakeAce Aug 16 '15 at 19:23overlay code, content -
As I understood , it all depends of the image, which is then: `_bitmap = await CanvasBitmap.LoadAsync(sender, "here");` So, can we send screen of current frame? Or is there another method? – SnakeAce Aug 16 '15 at 19:29
-
@SnakeAce, `LoadAsync` also takes `IRandomAccessStream`. So you could use `RenderTargetBitmap` to get the bitmap from the any `UIElement` on the UI, and then convert it into `InMemoryRandomAccessStream`. See my updated answer. – Justin XL Aug 16 '15 at 23:20
-
1See how to apply a more advanced blur [here](https://stackoverflow.com/questions/43699256/how-to-use-acrylic-accent-in-windows-10-creators-update/43972370#43972370). – Justin XL Aug 09 '17 at 04:08
You should look into the Win2D classes and in there for the Canvas Effects - there is a GaussianBlurEffect that might be helpful. Here's the reference: http://microsoft.github.io/Win2D/html/N_Microsoft_Graphics_Canvas_Effects.htm and for the GaussianBlurEffect: http://microsoft.github.io/Win2D/html/T_Microsoft_Graphics_Canvas_Effects_GaussianBlurEffect.htm including some C# code samples.
Addition: if you want to know how to use win2D, I just found a handy tutorial (which I am following myself right now :)) http://blogs.msdn.com/b/uk_faculty_connection/archive/2014/09/05/win2d.aspx

- 91
- 3
-
Haven't used the Win2D classes so can't say directly. But was wondering weather or not the method of using SetWindowCompositionAttribute might be better or worse from a performance standpoint? I know it uses Interop which might be a vote towards Win2D, but also seems very... Native, so to say :) (as you might hear, I haven't tested it myself, just know about it, if you haven't tested it yourself I'll experiment with it tomorrow and post my findings here) Example: http://withinrafael.com/adding-the-aero-glass-blur-to-your-windows-10-apps/ (sorry about the "In your face"-flag) – ILOABN Aug 13 '15 at 20:48