WPF 颜色间的转换 Color,Brush

前言

在工作中常用的颜色代码是这样的 #326CF3,当你想在后台代码中动态的改变UI某控件的相关颜色时就会用到String颜色与Color,Brush之间的转换问题。以下是一些常用示例。

示例

String To Color

复制代码
Color color = (Color)ColorConverter.ConvertFromString("#326CF3");

String To Brush

复制代码
BorderBack.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#326CF3"));

Brush brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#326CF3"));

纯色画笔(SolidColorBrush)

复制代码
Color color = (Color)ColorConverter.ConvertFromString("#326CF3");
Brush brush = new SolidColorBrush(color);

Brush TO Color:

复制代码
//方法1
Color color = (Color)ColorConverter.ConvertFromString("#326CF3");
Brush brush = new SolidColorBrush(color);
Color color1 = (Color)ColorConverter.ConvertFromString(brush.ToString());

//方法2
BorderBack.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#326CF3"));            
Color color2 = ((SolidColorBrush)BorderBack.Background).Color;

参数说明:

参数1:透明度

参数2:red数字(红)

参数3:green数字(绿)

参数4:blue数字(蓝)

Color color3 = Color.FromArgb(50, 10, 20, 30);

相关推荐
2601_962174171 天前
Redis 简介
数据库·redis·wpf
SamChan902 天前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf
SamChan902 天前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf
FuckPatience2 天前
WPF 拿到控件的最初样式,修改获得焦点样式
wpf
主宰者2 天前
【WPF】MainWindow前添加加载窗口,发现加载过程中有白色无内容窗口闪烁一下的解决方案
wpf
Vae_Mars3 天前
WPF中的ControlTemplate(控件模板)
wpf
SamChan903 天前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf
circuitsosk3 天前
多智能体协同在能源数据分析中的实践:分配-执行-校验闭环架构设计
python·数据分析·wpf·能源·并行计算·多智能体系统·agent协作
weixin199701080164 天前
《大促护航:电商API限流与降级,双11峰值500万调用的架构复盘》(附Python源码)
python·架构·wpf
hh9505 天前
gent Plan x DeepSeek Harness:多 Agent 协作场景下的中央编排实践
人工智能·wpf·adg·火山引擎·agent plan·adg成都社区