private void CleanUp() { effectBrush?.Dispose(); effectFactory?.Dispose(); glassEffect?.Dispose(); }
/// <inheritdoc cref="XamlCompositionBrushBase"/> protected override void OnDisconnected() { if (CompositionBrush != null) { _Brush?.Dispose(); _Factory?.Dispose(); _LuminanceEffect?.Dispose(); _InverseEffect?.Dispose(); CompositionBrush = null; } base.OnDisconnected(); }
private void UserControl_Unloaded(object sender, RoutedEventArgs e) { StopAnimating(); timer.Tick -= Timer_Tick; compositor.Dispose(); effectBrush.Dispose(); backdropBrush.Dispose(); effectFactory.Dispose(); hostVisual.Dispose(); if (glassVisual != null) { glassVisual.Dispose(); } }
public static void InitializeBlur(UIElement uiElement) { Visual hostVisual = ElementCompositionPreview.GetElementVisual(uiElement); Compositor compositor = hostVisual.Compositor; string name = "backdropBrush"; GaussianBlurEffect gausianBlur = new GaussianBlurEffect() { BlurAmount = 30f, BorderMode = EffectBorderMode.Hard, Source = new ArithmeticCompositeEffect() { MultiplyAmount = 0, Source1Amount = 1f, Source1 = new CompositionEffectSourceParameter(name), Source2Amount = 0f, Source2 = new ColorSourceEffect() { Color = Color.FromArgb(255, 245, 245, 245) } } }; CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(gausianBlur); CompositionBackdropBrush backdropBrush = compositor.CreateBackdropBrush(); CompositionEffectBrush effectBrush = effectFactory.CreateBrush(); effectBrush.SetSourceParameter(name, backdropBrush); SpriteVisual glassVisual = compositor.CreateSpriteVisual(); glassVisual.Brush = effectBrush; ElementCompositionPreview.SetElementChildVisual(uiElement, glassVisual); ExpressionAnimation bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size"); bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual); glassVisual.StartAnimation("Size", bindSizeAnimation); effectFactory.Dispose(); gausianBlur.Dispose(); backdropBrush.Dispose(); }
private void UpdateLightingEffect() { if (_effectFactory != null) { _effectFactory.Dispose(); _effectFactory = null; } _ambientLight.Targets.RemoveAll(); _pointLight.Targets.RemoveAll(); _distantLight.Targets.RemoveAll(); _spotLight.Targets.RemoveAll(); //ComboBoxItem item = LightingSelection.SelectedValue as ComboBoxItem; //switch ((LightingTypes)item.Tag) switch (SelectedLight) { case LightingTypes.PointDiffuse: { // // Result = Ambient + Diffuse // Result = (Image) + (.75 * Diffuse color) // UpdateLightingEffect_PointDiffuse(); // Set the light coordinate space and add the target Visual lightRoot = ElementCompositionPreview.GetElementVisual(gvMain); _pointLight.CoordinateSpace = lightRoot; _pointLight.Targets.Add(lightRoot); } break; case LightingTypes.PointSpecular: { // // Result = Ambient + Diffuse + Specular // Result = (Image * .6) + (Image * Diffuse color) + (Specular color) // UpdateLightingEffect_PointSpecular(); // Set the light coordinate space and add the target Visual lightRoot = ElementCompositionPreview.GetElementVisual(gvMain); _ambientLight.Targets.Add(lightRoot); _pointLight.CoordinateSpace = lightRoot; _pointLight.Targets.Add(lightRoot); } break; case LightingTypes.SpotLightDiffuse: { // // Result = Ambient + Diffuse // Result = Image + (Diffuse color * .75) // UpdateLightingEffect_SpotLightDiffuse(); // Set the light coordinate space and add the target Visual lightRoot = ElementCompositionPreview.GetElementVisual(gvMain); _spotLight.CoordinateSpace = lightRoot; _spotLight.Targets.Add(lightRoot); _spotLight.Direction = new Vector3(0, 0, -1); }; break; case LightingTypes.SpotLightSpecular: { // // Result = Ambient + Diffuse + Specular // Result = (Image * .6) + (Image * Diffuse color) + (Specular color) // UpdateLightingEffect_SpotLightSpecular(); // Set the light coordinate space and add the target Visual lightRoot = ElementCompositionPreview.GetElementVisual(gvMain); _ambientLight.Targets.Add(lightRoot); _spotLight.CoordinateSpace = lightRoot; _spotLight.Targets.Add(lightRoot); _spotLight.Direction = new Vector3(0, 0, -1); }; break; case LightingTypes.DistantDiffuse: { // // Result = Ambient + Diffuse // Result = (Image) + (.5 * Diffuse color) // UpdateLightingEffect_DistantDiffuse(); Visual lightRoot = ElementCompositionPreview.GetElementVisual(gvMain); _distantLight.CoordinateSpace = lightRoot; _distantLight.Targets.Add(lightRoot); }; break; case LightingTypes.DistantSpecular: { // // Result = Diffuse + Specular // Result = (Image * Diffuse color) + (Specular color) // UpdateLightingEffect_DistantSpecular(); Visual lightRoot = ElementCompositionPreview.GetElementVisual(gvMain); _distantLight.CoordinateSpace = lightRoot; _distantLight.Targets.Add(lightRoot); }; break; default: break; } //// Update the animations //UpdateAnimations(); // Update all the image to have the new effect UpdateEffectBrush(_currentBrushType); }