2016-12-12 43 views
1

我想合併模糊,飽和度和色調顏色。如何在UWP合成API中結合多種效果

我有一個Blend效果,包含GaussianBlur和Tint顏色。

_compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; 

var graphicsEffect = new BlendEffect 
{ 
    Mode = BlendEffectMode.Overlay, 
    Background = new GaussianBlurEffect() 
    { 
     Name = "Blur", 
     Source = new CompositionEffectSourceParameter("Backdrop"), 
     BlurAmount = 10f 
     BorderMode = EffectBorderMode.Hard, 
    }, 
    Foreground = new ColorSourceEffect() 
    { 
     Name = "Tint", 
     Color = Color.FromArgb(120, 255, 255, 255), 
    } 
}; 


var effectFactory = _compositor.CreateEffectFactory(graphicsEffect) 
_brush = effectFactory.CreateBrush(); 
_brush.SetSourceParameter("Backdrop", _compositor.CreateBackdropBrush()); 

_effectSprite = _compositor.CreateSpriteVisual(); 
_effectSprite.Size = new Vector2((float)this.ActualWidth, (float)this.ActualHeight) 
_effectSprite.Brush = _brush; 
ElementCompositionPreview.SetElementChildVisual(this, _effectSprite) 

我怎麼這個效果之前或之後增加SaturationEffect?

我試圖將它包裹到另一個BlendEffect中,並將另一個BackdropBrush設置爲SaturationEffect的源,但我僅獲得了白色背景。

我也試圖從SaturationEffect創建畫筆並設置高斯模糊的氨基酸來源,但我得到的例外「無效來源刷」

回答

4

下面是一個例子:

(input) Backdrop -> GaussianBlur ->| 
        ColorSource ->| Blend -> Saturation (output) 
var compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; 

var blurEffect = new GaussianBlurEffect() 
{ 
    Name = "Blur", 
    BlurAmount = 0.0f, 
    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", 10); 
brush.Properties.InsertColor("Tint.Color", Color.FromArgb(20, 255, 0, 0)); 
brush.Properties.InsertScalar("Saturation.Saturation", 1.5f); 

var sprite = compositor.CreateSpriteVisual(); 
sprite.Brush = brush; 
sprite.Offset = new Vector3(50, 250, 0); 
sprite.Size = new Vector2(200, 200); 
ElementCompositionPreview.SetElementChildVisual(this, sprite); 

Screenshot

+0

這很好,爲什麼它不被標記爲答案? – Sergio0694