File: Core\CSharp\System\Windows\Media\Effects\BlurBitmapEffect.cs
Project: wpf\src\PresentationCore.csproj (PresentationCore)
//------------------------------------------------------------------------------
//  Microsoft Avalon
//  Copyright (c) Microsoft Corporation, 2005
//
//  File:       BlurBitmapEffect.cs
//------------------------------------------------------------------------------
 
using MS.Internal;
using System;
using System.IO;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Media.Composition;
using MS.Internal.PresentationCore;
using System.Security;
 
 
namespace System.Windows.Media.Effects
{
    /// <summary>
    /// BlurBitmapEffectPrimitive
    /// </summary>
    public sealed partial class BlurBitmapEffect : BitmapEffect
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public BlurBitmapEffect()
        {
        }
 
        /// <summary>
        /// Creates the unmanaged effect handle
        /// </summary>
        /// <SecurityNote>
        /// Critical - returns a security critical type SafeHandle.
        /// Safe     - Always returns null.
        /// </SecurityNote>
        [SecuritySafeCritical]
        [Obsolete(MS.Internal.Media.VisualTreeUtils.BitmapEffectObsoleteMessage)]
        unsafe protected override SafeHandle CreateUnmanagedEffect()
        {
            return null;
        }
 
        /// <summary>
        /// Update (propagetes) properties to the unmanaged effect
        /// </summary>    
        /// <SecurityNote>
        /// This method demands permission because effects should not be run
        /// in partial trust.
        /// 
        /// SecurityCritical - because SetValue has a link demand
        /// SecutiryTreatAsSafe - because it demans UIWindow permission
        /// </SecurityNote>
        [SecurityCritical, SecurityTreatAsSafe]
        [Obsolete(MS.Internal.Media.VisualTreeUtils.BitmapEffectObsoleteMessage)]
        protected override void UpdateUnmanagedPropertyState(SafeHandle unmanagedEffect)
        {
            SecurityHelper.DemandUIWindowPermission();
        }
 
        /// <summary>
        /// An ImageEffect can be used to emulate a BlurBitmapEffect with certain restrictions. This
        /// method returns true when it is possible to emulate the BlurBitmapEffect using an ImageEffect.
        /// </summary>
        internal override bool CanBeEmulatedUsingEffectPipeline()
        {
            return (Radius <= 100.0);
        }
 
        /// <summary>
        /// Returns a Effect that emulates this BlurBitmapEffect.
        /// </summary>        
        internal override Effect GetEmulatingEffect()
        {
            if (_imageEffectEmulation != null && _imageEffectEmulation.IsFrozen)
            {
                return _imageEffectEmulation;
            }
            
            if (_imageEffectEmulation == null)
            {
                _imageEffectEmulation = new BlurEffect();
            }
 
            double radius = Radius;
            if (_imageEffectEmulation.Radius != radius)
            {
                _imageEffectEmulation.Radius = radius;
            }
 
            KernelType kernelType = KernelType;
            if (_imageEffectEmulation.KernelType != kernelType)
            {
                _imageEffectEmulation.KernelType = kernelType;
            }
 
            _imageEffectEmulation.RenderingBias = RenderingBias.Performance;
 
            if (this.IsFrozen)
            {
                _imageEffectEmulation.Freeze();
            }
            
            return _imageEffectEmulation;
        }        
 
        BlurEffect _imageEffectEmulation;
    }
}