C# WPF编程-画刷(Brush)填充图形对象的颜色或图案

devtools/2025/3/18 10:45:20/

C# WPF 画刷(Brush)填充图形对象的颜色或图案

  • 1.SolidColorBrush
  • 2.LinearGradientBrush
  • 3. RadialGradientBrush
  • 5. DrawingBrush
  • 6. VisualBrush
  • 综合示例

1.SolidColorBrush

  • SolidColorBrush是最简单的画刷类型,用于以纯色填充区域。
<Rectangle Width="100" Height="100"><Rectangle.Fill><SolidColorBrush Color="Blue"/></Rectangle.Fill>
</Rectangle>

或者代码实现

var rectangle = new Rectangle { Width = 100, Height = 100 };
rectangle.Fill = new SolidColorBrush(Colors.Blue);

在这里插入图片描述

2.LinearGradientBrush

LinearGradientBrush用于创建线性渐变效果,可以指定多个颜色停止点(GradientStop)来定义渐变的颜色过渡。

<Rectangle Width="200" Height="100"><Rectangle.Fill><LinearGradientBrush StartPoint="0,0" EndPoint="1,1"><GradientStop Color="Yellow" Offset="0.0" /><GradientStop Color="Red" Offset="0.25" /><GradientStop Color="Blue" Offset="0.75" /><GradientStop Color="LimeGreen" Offset="1.0" /></LinearGradientBrush></Rectangle.Fill>
</Rectangle>

在这里插入图片描述

3. RadialGradientBrush

RadialGradientBrush与LinearGradientBrush类似,但它创建的是基于圆心向外辐射的渐变效果。

<Ellipse Width="200" Height="100"><Ellipse.Fill><RadialGradientBrush Center="0.5,0.5" GradientOrigin="0.5,0.5" RadiusX="0.5" RadiusY="0.5"><GradientStop Color="Yellow" Offset="0" /><GradientStop Color="Red" Offset="0.25" /><GradientStop Color="Blue" Offset="0.75" /><GradientStop Color="LimeGreen" Offset="1" /></RadialGradientBrush></Ellipse.Fill>
</Ellipse>

在这里插入图片描述
4. ImageBrush
ImageBrush允许使用图像作为填充内容。

<Rectangle Width="200" Height="100"><Rectangle.Fill><ImageBrush ImageSource="Images/Linux-logo.png"/></Rectangle.Fill>
</Rectangle>

在这里插入图片描述

5. DrawingBrush

DrawingBrush可用于绘制矢量图形或位图作为填充内容。

<Rectangle Width="200" Height="100"><Rectangle.Fill><DrawingBrush><DrawingBrush.Drawing><GeometryDrawing Geometry="M0,0 L1,0 0,1 Z"><GeometryDrawing.Brush><SolidColorBrush Color="Red"/></GeometryDrawing.Brush></GeometryDrawing></DrawingBrush.Drawing></DrawingBrush></Rectangle.Fill>
</Rectangle>

在这里插入图片描述

6. VisualBrush

VisualBrush允许你用另一个UI元素的内容作为填充内容。

<Rectangle Width="200" Height="100"><Rectangle.Fill><VisualBrush><VisualBrush.Visual><TextBlock Text="Hello, WPF!" FontSize="20"/></VisualBrush.Visual></VisualBrush></Rectangle.Fill>
</Rectangle>

在这里插入图片描述

综合示例

<Window x:Class="WpfBaseDemo.Window1"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:WpfBaseDemo"mc:Ignorable="d"Title="Window1" Height="450" Width="800"><Grid><!-- 定义行 --><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><!-- 定义列 --><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Rectangle Grid.Row="0" Grid.Column="0"><Rectangle.Fill><SolidColorBrush Color="Blue" Opacity="0.8"/></Rectangle.Fill></Rectangle><Rectangle Grid.Row="0" Grid.Column="1"><Rectangle.Fill><LinearGradientBrush StartPoint="0 0" EndPoint="1 1"><GradientStop Offset="0.00" Color="Yellow"/><GradientStop Offset="0.25" Color="Red"/><GradientStop Offset="0.50" Color="Blue"/><GradientStop Offset="0.75" Color="LimeGreen"/></LinearGradientBrush></Rectangle.Fill></Rectangle><Ellipse Grid.Row="0" Grid.Column="2"><Ellipse.Fill><RadialGradientBrush Center="0.5 0.5" GradientOrigin="0.5 0.5" RadiusX="0.5" RadiusY="0.5"><GradientStop Offset="0.00" Color="Yellow"/><GradientStop Offset="0.25" Color="Red"/><GradientStop Offset="0.75" Color="Blue"/><GradientStop Offset="1.00" Color="LimeGreen"/></RadialGradientBrush></Ellipse.Fill></Ellipse><Rectangle Grid.Row="1" Grid.Column="0"><Rectangle.Fill><ImageBrush ImageSource="Images/Linux-logo.png" Stretch="Uniform" AlignmentX="Left" AlignmentY="Top"/></Rectangle.Fill></Rectangle><Rectangle Grid.Row="1" Grid.Column="1"><Rectangle.Fill><DrawingBrush><DrawingBrush.Drawing><GeometryDrawing Geometry="M0,0 L1,0 0,1 Z"><GeometryDrawing.Brush><SolidColorBrush Color="Red"/></GeometryDrawing.Brush></GeometryDrawing></DrawingBrush.Drawing></DrawingBrush></Rectangle.Fill></Rectangle><Rectangle Grid.Row="1" Grid.Column="2"><Rectangle.Fill><VisualBrush><VisualBrush.Visual><Canvas><TextBlock Text="Hello" FontSize="20" Opacity="0.6" Foreground="Red"/><Label Content="Hello"></Label></Canvas></VisualBrush.Visual></VisualBrush></Rectangle.Fill></Rectangle></Grid>
</Window>

在这里插入图片描述


http://www.ppmy.cn/devtools/168041.html

相关文章

操作系统八股文整理(一)

操作系统八股文整理 一、进程和线程的区别二、进程与线程的切换过程一、进程切换进程切换的步骤&#xff1a; 二、线程切换线程切换的步骤&#xff1a; 三、进程切换与线程切换的对比四、上下文切换的优化 三、系统调用一、系统调用的触发二、从用户空间切换到内核空间三、执行…

从零搭建微服务项目Pro(第6-1章——Spring Security+JWT实现用户鉴权访问与token刷新)

前言&#xff1a; 在现代的微服务架构中&#xff0c;用户鉴权和访问控制是非常重要的一部分。Spring Security 是 Spring 生态中用于处理安全性的强大框架&#xff0c;而 JWT&#xff08;JSON Web Token&#xff09;则是一种轻量级的、自包含的令牌机制&#xff0c;广泛用于分…

Python 中用T = TypeVar(“T“)这个语法定义一个“类型变量”,属于类型提示系统的一部分

T TypeVar("T") 这一语法规则定义了一个泛型类型变量 T&#xff0c;用于标记“某种类型”&#xff0c;让你可以写出既通用又类型安全的代码。 TypeVar(“T”) 会创建一个名为 T 的类型占位符&#xff0c;这个占位符可以在后续的函数、类或方法中用作泛型参数。泛型…

提高开发效率:公共字段自动化填充方案

目录 问题描述 解决方案 Mybatis-Plus AOP注解 方案比较 MyBatis-Plus 自动填充功能 AOP 注解 问题描述 当我们在开发过程中&#xff0c;处理像创建时间、创建人等这样的公共字段会显得比较繁琐&#xff0c;尤其是在每个实体都需要这些信息的情况下。就比如&#xff0c…

spring启动流程

Spring启动流程 随着springboot的功能越来越强大&#xff0c;我们逐渐忘记了spring&#xff0c;但是每当遇到问题时缺无从下手&#xff0c; 我们在享受springboot给我们带来的便利的同时更应该了解其底层原理&#xff0c;知其然更要知其所以然&#xff0c;下面我们一起进入spr…

[023-01-47].第47节:组件应用 - GetWay与 Sentinel 集成实现服务限流

SpringCloud学习大纲 一、需求说明&#xff1a; 实现网关cloudalibaba-sentinel-gateway9528模块保护cloudalibaba-provider-payment9001 二、编码实现&#xff1a; 2.1.建module: 新建模块&#xff0c;名称是&#xff1a;cloudalibaba-sentinel-gateway9528 2.2.改pom &l…

电池预测 | 第23讲 基于CNN-BiLSTM的锂电池剩余寿命预测,附带PPT视频讲解

电池预测 | 第23讲 基于CNN-BiLSTM的锂电池剩余寿命预测&#xff0c;附带PPT视频讲解 目录 电池预测 | 第23讲 基于CNN-BiLSTM的锂电池剩余寿命预测&#xff0c;附带PPT视频讲解预测效果基本描述程序设计参考资料 预测效果 基本描述 电池预测 | 第23讲 基于CNN-BiLSTM的锂电池…

【每日学点HarmonyOS Next知识】图片拖动、动画放大、列表高度、返回键监听、分割线颜色

1、HarmonyOS image 组件的图片可以随意拖动&#xff1f; mage组件默认拖拽效果&#xff0c;draggable设置为true时&#xff0c;组件可拖拽&#xff0c;参考以下文档&#xff1a;https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/image-V5 HarmonyOS中…