#559 – Drawing a Masked Gradient in Blend
May 16, 2012 Leave a comment
You can use an opacity mask with simple shapes along with a gradient to get a style that looks like a gradient applied to a paper cutout. For example, you might want the following cloud-like effect.
To do this, start with a Rectangle and use a linear gradient for its Fill property.
<Rectangle Height="400" Stroke="Black" Width="600"> <Rectangle.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFFBFBFB" Offset="0.504"/> <GradientStop Color="#FFD6E1EA" Offset="0.734"/> <GradientStop Color="#FF6EB5F5" Offset="1"/> </LinearGradientBrush> </Rectangle.Fill> </Rectangle>
Now create an image with the shape of the clouds, using an opaque foreground (black in this example) and a transparent background (shown as checkerboard here).
Finally, use the new clouds image as an ImageBrush for the OpacityMask of the Rectangle.
<Rectangle Height="400" Stroke="Black" Width="600"> <Rectangle.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFFBFBFB" Offset="0.504"/> <GradientStop Color="#FF6EB5F5" Offset="1"/> <GradientStop Color="#FFD6E1EA" Offset="0.734"/> </LinearGradientBrush> </Rectangle.Fill> <Rectangle.OpacityMask> <ImageBrush ImageSource="Images\CloudsMask.png"/> </Rectangle.OpacityMask> </Rectangle>