一个颜色转换像#XXXXXX一个字符串System.Windows.Media.Brush Simpliest方式 [英] Simpliest way to convert a Color as a string like #XXXXXX to System.Windows.Media.Brush

查看:4336
本文介绍了一个颜色转换像#XXXXXX一个字符串System.Windows.Media.Brush Simpliest方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得标题是明确的。

我现在拥有的是:

System.Drawing.Color uiui = System.Drawing.ColorTranslator.FromHtml(myString);
var intColor = (uint)((uiui.A << 24) | (uiui.R << 16) | (uiui.G << 8) | (uiui.B << 0));
var bytes = BitConverter.GetBytes(uint.Parse(value));
var brush = new SolidColorBrush();
brush.Color = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);



1的myString就像#FFFFFF就像我在标题中说

1- myString is like #FFFFFF like I said in the title

2 - 这失败的BitConverter.GetBytes线,让我吃惊,因为我得到了我的颜色代表INT!

2- This fails on the BitConverter.GetBytes line which surprises me cause I got the int representation on my Color !

3总之,我知道,颜色转换没有那么直观,但我觉得我不这样做是正确的......
是好方法?

3- Anyway, I know that COlor conversion are not that intuitive but I feel like I'm not doing it right... Is that the good way ?

推荐答案

您可以使用的 System.Windows.Media.ColorConverter

You can use the System.Windows.Media.ColorConverter

var color = (Color)ColorConverter.ConvertFromString("#FF010203");
//OR
var color = (Color)ColorConverter.ConvertFromString("#010203");
//OR
var color = (Color)ColorConverter.ConvertFromString("Red");

//and then:
var brush = new SolidColorBrush(color);  



这是因为什么它接受非常灵活。看一看在XAML的例子这里。你可以通过任何这些字符串格式的

It's pretty flexible as to what it accepts. Have a look at the examples in XAML here. You can pass any of those string formats in.

请注意:这些都是在 System.Windows.Media (为WPF),不要与 System.Drawing中混淆(为的WinForms)

Note: These are all in System.Windows.Media (for WPF) not to be confused with System.Drawing (for WinForms)

这篇关于一个颜色转换像#XXXXXX一个字符串System.Windows.Media.Brush Simpliest方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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