Provides Lightweight alternatives for dropshadowbitmapeffect in WPF
Zhou yinhui
WPF has an exciting new feature: We can easily provide bitmapeffect for visual elements, such as shadows. however, it is frustrating that bitmap results consume a lot of CPU resources. From the usual development, we can come up with two experiences: 1. Use bitmap effects as little as possible, because it is calculated by the CPU rather than the GPU. 2. Do not use bitmap effects with animations that are a little more complex. It often makes the animation not smooth.
In the bitmap effect, dropshadowbitmapeffect is the most common one. Here, systemdropshadowbitmapeffect, a substitute for dropshadowchrome, is not perfect, however, it is often a good solution to dropshadowbitmapeffect performance problems.
We can use a systemdropshadowchrome object that will act as the "shadow" and then use the object to add the shadow as the child of the systemdropshadowchrome object, which is the same as a border object.
In, the shadow effect of the pale blue square is obtained using our systemdropshadowchrome. The shadow effect of the Orange square is obtained using dropshadowbitmapeffect. You can paste the following Code View on xamlpad: < Page Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: = "CLR-namespace: Microsoft. Windows. themes; Assembly = presentationframework. Aero" >
< Grid Width = "579" Height = "492" >
< A: systemdropshadowchrome Width = "75" Cornerradius = "3,13, 3,13" Rendertransformorigin = "0.5, 0.5" Horizontalalignment = "Left" Margin = "185,149" X: Name = "Systemdropshadowchrome" Verticalalignment = "TOP" Height = "75" >
< Border Background = "Lightblue" Cornerradius = "3,13, 3,13" Margin = "1, 1, 1" />
</ A: systemdropshadowchrome >
< Border Background = "# Ffe78e16" Cornerradius = "3,13, 3,13" Rendertransformorigin = "0.5, 0.5" Horizontalalignment = "Left" Margin = "0,132" X: Name = "Border" Verticalalignment = "Bottom" Width = "75" Height = "75" >
< Border . Bitmapeffect >
< Dropshadowbitmapeffect Shadowdepth = "2" />
</ Border. bitmapeffect >
</ Border >
</ Grid >
</ Page >
In addition, the color of the shadow can be set. You only need to set the color attribute of the systemdropshadowchrome object.
Unfortunately, the systemdropshadowchrome object cannot set shadows for elements in other shapes, because its own shape can only be square or rounded.
You can click here or here to view more discussions about systemdropshadowchrome.