从十六进制颜色值创建 SolidColorBrush [英] Creating SolidColorBrush from hex color value

查看:29
本文介绍了从十六进制颜色值创建 SolidColorBrush的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从十六进制值(例如#ffacc)创建 SolidColorBrush.我该怎么做?

I want to create SolidColorBrush from Hex value such as #ffaacc. How can I do this?

在 MSDN 上,我得到:

On MSDN, I got :

SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);

所以我写了(考虑到我的方法接收颜色为 #ffaacc):

So I wrote (considering my method receives color as #ffaacc):

Color.FromRgb(
  Convert.ToInt32(color.Substring(1, 2), 16), 
  Convert.ToInt32(color.Substring(3, 2), 16), 
  Convert.ToInt32(color.Substring(5, 2), 16));

但这给出了错误

'System.Windows.Media.Color.FromRgb(byte, byte, byte)'的最佳重载方法匹配有一些无效参数

还有 3 个错误:Cannot convert int to byte.

但是 MSDN 示例是如何工作的?

But then how MSDN example works?

推荐答案

试试这个:

(SolidColorBrush)new BrushConverter().ConvertFrom("#ffaacc");

这篇关于从十六进制颜色值创建 SolidColorBrush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆