Bitmap Effects Overview
Bitmap effects enable designers and developers to apply visual effects to rendered Windows Presentation Foundation (WPF) content. For example, bitmap effects allow you to easily apply a DropShadowBitmapEffect effect or a blur effect to an image or a button.
Bitmap effects (BitmapEffect object) are simple pixel processing operations. A bitmap effect takes a BitmapSource as an input and produces a new BitmapSource after applying the effect, such as a blur or drop shadow. Each bitmap effect exposes properties that can control the filtering properties, such as Radius of BlurBitmapEffect.
As a special case, in WPF, effects can be set as properties on live Visual objects, such as a Button or TextBox. The pixel processing is applied and rendered at run-time. In this case, at the time of rendering, a Visual is automatically converted to its BitmapSource equivalent and is fed as input to the BitmapEffect. The output replaces the Visual object's default rendering behavior. This is why BitmapEffect objects force visuals to render in software only i.e. no hardware acceleration on visuals when effects are applied.
BlurBitmapEffect simulates an object that appears out-of-focus.
OuterGlowBitmapEffect creates a halo of color around the perimeter of an object.
DropShadowBitmapEffect creates a shadow behind an object.
BevelBitmapEffect creates a bevel which raises the surface of an image according to a specified curve.
WPF bitmap effects are rendered in software mode. Any object that applies an effect will also be rendered in software. Performance is degraded the most when using Bitmap effects on large visuals or animating properties of a Bitmap effect. This is not to say that you should not use Bitmap effects in this way at all, but you should use caution and test thoroughly to ensure that your users are getting the experience you expect.
WPF bitmap effects do not support partial trust execution. An application must have full trust permissions to use bitmap effects.
BitmapEffect is a property on Visual. Therefore applying effects to Visuals, such as a Button, Image, DrawingVisual, or UIElement, is as easy as setting a property. BitmapEffect can be set to a single BitmapEffect object or multiple effects can be chained by using the BitmapEffectGroup object.
The following example demonstrates how to apply a BitmapEffect in Extensible Application Markup Language (XAML).
<Button Width="200">You Can't Read This! <Button.BitmapEffect> <!-- <BitmapEffectGroup> would go here if you wanted to apply more then one effect to the Button. However, in this example only one effect is being applied so BitmapEffectGroup does not need to be included. --> <!-- The larger the Radius, the more blurring. The default range is 20. In addition, the KernelType is set to a box kernel. A box kernel creates less disruption (less blur) then the default Gaussian kernel. --> <BlurBitmapEffect Radius="10" KernelType="Box" /> </Button.BitmapEffect> </Button>
The following example demonstrates how to apply a BitmapEffect in code.
// Get a reference to the Button. Button myButton = (Button)sender; // Initialize a new BlurBitmapEffect that will be applied // to the Button. BlurBitmapEffect myBlurEffect = new BlurBitmapEffect(); // Set the Radius property of the blur. This determines how // blurry the effect will be. The larger the radius, the more // blurring. myBlurEffect.Radius = 10; // Set the KernelType property of the blur. A KernalType of "Box" // creates less blur than the Gaussian kernal type. myBlurEffect.KernelType = KernelType.Box; // Apply the bitmap effect to the Button. myButton.BitmapEffect = myBlurEffect;
WPF also provides unmanaged interfaces to create custom effects that can be used in managed WPF applications. For additional reference material for creating custom bitmap effects, see the Unmanaged WPF Bitmap Effect documentation.