简体   繁体   中英

Gaussian Blur Effect on UWP can't brush?

I try to follow the tutorial of windows developer UWP, and get to this point

public sealed partial class MainPage : Page
{
    private Compositor compositor = Window.Current.Compositor;
    private SpriteVisual effectVisual;
    private CompositionEffectBrush effectBrush;

    public MainPage()
    {
        this.InitializeComponent();
    }

    private void BlurAmount_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        var blur_amount = (float)e.NewValue;


    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        effectVisual = compositor.CreateSpriteVisual();

        var graphicsEffect = new GaussianBlurEffect
        {
            BlurAmount = 100f,
            BorderMode = EffectBorderMode.Hard,
            Source = new CompositionEffectSourceParameter("Background")
        };

        var effectFactory = compositor.CreateEffectFactory(graphicsEffect);
        effectBrush = effectFactory.CreateBrush();

        var destinationBrush = compositor.CreateBackdropBrush();
        effectBrush.SetSourceParameter("Background", destinationBrush);

        effectVisual.Brush = effectBrush;

        ElementCompositionPreview.SetElementChildVisual(theGrid, effectVisual);
    }
}

And here is the UI I made, I want to blur the grid in the center of the root grid which an Image Background on it

<Grid x:Name="Back">
    <Grid.Background>
        <ImageBrush ImageSource="Assets/1.png"/>
    </Grid.Background>
    <Grid x:Name="theGrid" VerticalAlignment="Center" HorizontalAlignment="Center"
          Width="100" Height="100" ></Grid>
    <Slider x:Name="BlurAmount" ValueChanged="BlurAmount_ValueChanged" Maximum="100" Minimum="0"/>
</Grid>

His code and mine are the same, I rechecked a thousand time but my blur effect not apply to the grid Did I miss something ?

I think it is necessary to specify the size of the visual, for example:

blurSprite.Size = new Vector2((float)theGrid.ActualWidth, (float)theGrid.ActualHeight);

I have no way to test right now, but this should help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM