// 给按钮增加投影,图片增加模糊效果
<Window x:Class="WpfGraphical.BitmapWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfGraphical"
mc:Ignorable="d"
Title="BitmapWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="120*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--DropShadowEffect来给按钮添加投影效果。通过设置ShadowDepth属性来控制阴影的深度,BlurRadius属性来控制阴影的模糊程度,Color属性来设置阴影的颜色,Opacity属性设置透明度-->
<Button Grid.Row="0" Grid.Column="0" Content="点击我" Width="100" Height="30">
<Button.Effect>
<DropShadowEffect ShadowDepth="10" BlurRadius="10" Color="red" Opacity="0.8" />
</Button.Effect>
</Button>
<StackPanel Grid.Row="1" Grid.Column="0">
<!-- 图片设置模糊效果并模糊半径为40像素-->
<Image Stretch="Fill" VerticalAlignment="top" Source="./1.png">
<Image.Effect>
<BlurEffect Radius="40"/>
</Image.Effect>
</Image>
</StackPanel>
</Grid>
</Window>
// 使用SolidColorBrush 纯色填充元素,和文本背景颜色和字体颜色
<Window x:Class="WpfGraphical.SolidColorBrushWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfGraphical"
mc:Ignorable="d"
Title="SolidColorBrushWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--绘制矩形并填充红色-->
<Rectangle Grid.Column="0" Grid.Row="0" Width="200" Height="100" VerticalAlignment="Stretch">
<Rectangle.Fill>
<SolidColorBrush Color="Red" />
</Rectangle.Fill>
</Rectangle>
<!--绘制圆形并填充蓝色-->
<Ellipse Grid.Column="0" Grid.Row="1" Width="200" Height="200" VerticalAlignment="Stretch">
<Ellipse.Fill>
<SolidColorBrush Color="Blue" />
</Ellipse.Fill>
</Ellipse>
<StackPanel Grid.Column="0" Grid.Row="2">
<TextBlock>
<TextBlock.Background>
<SolidColorBrush Color="Yellow"></SolidColorBrush>
</TextBlock.Background>
<TextBlock.Foreground>
<SolidColorBrush Color="Red" />
</TextBlock.Foreground>
SolidColorBrush 设置文本背景颜色和字体颜色
</TextBlock>
</StackPanel>
</Grid>
</Window>
// 使用LinearGradientBrush 渐变色设置矩形四种颜色渐变,圆形两种颜色
// LinearGradientBrush的StartPoint设置为左上角(0,0),EndPoint设置为右下角(1,1),渐变将从左上角开始,沿对角线方向渐变到右下角
// Offset的值范围是0到1,表示颜色的渐变位置。Offset为0表示渐变的起点,Offset为1表示渐变的终点
<Window x:Class="WpfGraphical.LinearGradientBrushWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfGraphical"
mc:Ignorable="d"
Title="LinearGradientBrushWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--LinearGradientBrush的StartPoint设置为左上角(0,0),EndPoint设置为右下角(1,1),渐变将从左上角开始,沿对角线方向渐变到右下角-->
<Rectangle Grid.Column="0" Grid.Row="0" Width="200" Height="100" VerticalAlignment="Stretch">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<LinearGradientBrush.GradientStops>
<!--Offset的值范围是0到1,表示颜色的渐变位置。Offset为0表示渐变的起点,Offset为1表示渐变的终点,-->
<!--定义四个颜色不同位置-->
<GradientStop Offset="0" Color="Red" />
<GradientStop Offset="0.5" Color="Blue" />
<GradientStop Offset="0.8" Color="Black" />
<GradientStop Offset="1" Color="Green" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Ellipse Grid.Column="0" Grid.Row="1" Width="200" Height="200" VerticalAlignment="Stretch">
<Ellipse.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<LinearGradientBrush.GradientStops>
<!--定义两种颜色-->
<GradientStop Offset="0" Color="Red" />
<GradientStop Offset="1" Color="Green" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Grid>
</Window>
<Window x:Class="WpfGraphical.RadialGradientBrushWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfGraphical"
mc:Ignorable="d"
Title="RadialGradientBrushWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--RadialGradientBrush的StartPoint设置为左上角(0,0),EndPoint设置为右下角(1,1),渐变将从左上角开始,沿对角线方向渐变到右下角-->
<!--GradientOrigin 渐变开始的坐标0.5,0.5 就是中心点,并且设置 X坐标0 ,Y坐标0.5-->
<Rectangle Grid.Column="0" Grid.Row="0" Width="200" Height="100" VerticalAlignment="Stretch">
<Rectangle.Fill>
<RadialGradientBrush GradientOrigin="0.5,0.5">
<RadialGradientBrush.Center>
<Point X="0" Y="0.5"></Point>
</RadialGradientBrush.Center>
<RadialGradientBrush.GradientStops>
<!--Offset的值范围是0到1,表示颜色的渐变位置。Offset为0表示渐变的起点,Offset为1表示渐变的终点,-->
<!--定义四个颜色不同位置-->
<GradientStop Offset="0" Color="Red" />
<GradientStop Offset="0.5" Color="Blue" />
<GradientStop Offset="0.8" Color="Black" />
<GradientStop Offset="1" Color="Green" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Ellipse Grid.Column="0" Grid.Row="1" Width="200" Height="200" VerticalAlignment="Stretch">
<Ellipse.Fill>
<RadialGradientBrush>
<RadialGradientBrush.GradientStops>
<!--定义两种颜色-->
<GradientStop Offset="0" Color="Red" />
<GradientStop Offset="1" Color="Green" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Grid>
</Window>
// 使用 ImageSource 来设置背景图
<Window x:Class="WpfGraphical.ImageBrushWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfGraphical"
mc:Ignorable="d"
Title="ImageBrushWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--矩形里填充一张图片-->
<Rectangle Grid.Column="0" Grid.Row="0" Width="200" Height="100" VerticalAlignment="Stretch">
<Rectangle.Fill>
<ImageBrush ImageSource="./1.png">
</ImageBrush>
</Rectangle.Fill>
</Rectangle>
<!--设置按钮控件的背景图-->
<Button Grid.Column="0" Grid.Row="1" Width="250" Height="50" Content="我是一个有背景图的按钮" FontSize="20" Foreground="White">
<Button.Background>
<ImageBrush ImageSource="./1.png" Opacity="0.5">
</ImageBrush>
</Button.Background>
</Button>
<!--设置文本的背景图,为了方便看清,图片增加了个透明度-->
<TextBlock Foreground="Red" Grid.Column="0" Grid.Row="2" Height="30" FontSize="24">
<TextBlock.Background>
<ImageBrush ImageSource="./1.png" Opacity="0.5">
</ImageBrush>
</TextBlock.Background>
我是一个有背景的文本
</TextBlock>
</Grid>
</Window>
// 使用 DrawingBrush 绘制
<Window x:Class="WpfGraphical.DrawingBrushWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfGraphical"
mc:Ignorable="d"
Title="DrawingBrushWindow" Height="450" Width="800">
<Grid>
<!--绘制一个矩形, RectangleGeometry 设置矩形坐标(0,0)宽度和高度为(200,100) -->
<Rectangle Width="200" Height="100">
<Rectangle.Fill>
<DrawingBrush>
<DrawingBrush.Drawing>
<DrawingGroup>
<!--绘制一个矩形设置矩形坐标(0,0)宽度和高度为(200,100)-->
<GeometryDrawing Brush="Red">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,200,100"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
<!--绘制一个矩形设置矩形坐标(10,20)宽度和高度为(50,20)-->
<GeometryDrawing Brush="Green">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="10,20,50,20"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
<!--绘制一个矩形设置矩形坐标(10,50)宽度和高度为(60,40) 填充图片-->
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="10,50,60,40"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<ImageBrush ImageSource="./1.png">
</ImageBrush>
</GeometryDrawing.Brush>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
// VisualBrush 绘制
<Window x:Class="WpfGraphical.VisualBrushWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfGraphical"
mc:Ignorable="d"
Title="VisualBrushWindow" Height="450" Width="800">
<Grid>
<Rectangle Width="300" Height="300">
<Rectangle.Fill>
<VisualBrush TileMode="Tile">
<VisualBrush.Visual>
<StackPanel>
<StackPanel.Background>
<!--使用自制画刷绘制图形-->
<DrawingBrush>
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Brush>
<RadialGradientBrush>
<GradientStop Color="red" Offset="0.0" />
<GradientStop Color="Green" Offset="1.0" />
</RadialGradientBrush>
</GeometryDrawing.Brush>
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50" />
<RectangleGeometry Rect="50,50,50,50" />
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</StackPanel.Background>
<!--添加一个文字-->
<TextBlock FontSize="10" Margin="10">这里是文字</TextBlock>
<!--添加一个图片-->
<Image Source="./1.png" Height="40" Width="40"></Image>
<!--按钮-->
<Button Content="我是是一个按钮" Margin="10"> </Button>
</StackPanel>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
公众号“点滴分享技术猿”