如何在给定框架元素的情况下检索 CompositionEffect



我正在试验一个链接效果,并将其设置在图像控件(TestImage(上,如下所示:

    private void TestImage_ImageOpened(object sender, RoutedEventArgs e)
    {
        /*
            (input) Backdrop -> GaussianBlur -> |
                                 ColorSource -> | Blend -> Saturation (output)
         */
        var compositor = ElementCompositionPreview.GetElementVisual(this.TestImage).Compositor;
        var blurEffect = new GaussianBlurEffect()
        {
            Name = "Blur",
            BlurAmount = 10f,
            BorderMode = EffectBorderMode.Hard,
            Optimization = EffectOptimization.Balanced,
            Source = new CompositionEffectSourceParameter("source"),
        };
        var colorEffect = new ColorSourceEffect
        {
            Name = "Tint",
        };
        var blendEffect = new BlendEffect
        {
            Background = blurEffect,
            Foreground = colorEffect,
            Mode = BlendEffectMode.Overlay,
        };
        var saturationEffect = new SaturationEffect
        {
            Name = "Saturation",
            Source = blendEffect,
            Saturation = 1.5f,
        };
        var factory = compositor.CreateEffectFactory(saturationEffect, new[]
        {
            "Blur.BlurAmount",
            "Tint.Color",
            "Saturation.Saturation",
        });
        var brush = factory.CreateBrush();
        brush.SetSourceParameter("source", compositor.CreateBackdropBrush());
        // Animatable properties
        brush.Properties.InsertScalar("Blur.BlurAmount", 10f);
        brush.Properties.InsertColor("Tint.Color", Color.FromArgb(128, 255, 0, 0));
        brush.Properties.InsertScalar("Saturation.Saturation", 1.5f);
        var sprite = compositor.CreateSpriteVisual();
        sprite.Brush = brush;
        sprite.Size = new Vector2((float)this.TestImage.ActualWidth, (float)this.TestImage.ActualHeight);
        ElementCompositionPreview.SetElementChildVisual(this.TestImage, sprite);
    }

我的目标是稍后检索该效果,以便我可以读取有关该效果的一些值。如何仅使用 TestImage 控件和合成图层获得效果?

我使用附加属性解决了我的问题,因为此功能本身不受支持:

internal static SpriteVisual GetSprite(DependencyObject obj) => (SpriteVisual)obj.GetValue(SpriteProperty);
internal static void SetSprite(DependencyObject obj, SpriteVisual value) => obj.SetValue(SpriteProperty, value);
/// <summary>
/// Composition Sprite attached to a FrameworkElement.
/// </summary>
internal static readonly DependencyProperty SpriteProperty =
    DependencyProperty.RegisterAttached(
        "Sprite",
        typeof(SpriteVisual),
        typeof(MyAnimations),
        new PropertyMetadata(null));

相关内容

  • 没有找到相关文章

最新更新