C#中Brush、Color、String相互转换

在编写cs的前端界面显示代码时,经常会涉及颜色的变化,一般是使用#DDDDDD这种16进制的方式和Color.FromRgb(221, 221, 221)这样的方式来表达。--转载者注

1、String转换成Color

            Color color = (Color)ColorConverter.ConvertFromString(string);

2、String转换成Brush

            BrushConverter brushConverter = new BrushConverter();
            Brush brush = (Brush)brushConverter.ConvertFromString(string);

3、Color转换成Brush

            Brush brush = new SolidColorBrush(color));

4、Brush转换成Color有两种方法:

1)先将Brush转成string,再转成Color。

            Color color= (Color)ColorConverter.ConvertFromString(brush.ToString());

2)将Brush转成SolidColorBrush,再取Color。

            Color color= ((SolidColorBrush)CadColor.Background).Color;

 

转载于:https://www.cnblogs.com/zwnet/archive/2012/06/27/2565777.html

posted @ 2021-12-20 15:48  苏州の酱醋茶  阅读(892)  评论(0编辑  收藏  举报