WPF color、brush、string转换

//color转为brush:
Brush br = new SolidColorBrush(Color.FromRgb(0,0,0)); 
//string转Color
(Color)ColorConverter.ConvertFromString((string)str);
//Color转string((Color)value).ToString();
string和Brush的转换
Brush color = newSolidColorBrush((Color)ColorConverter.ConvertFromString((string)str)); 
//Brush转string
((Brush)value).ToString();
//string转byte[]
System.Text.UnicodeEncoding converter = newSystem.Text.UnicodeEncoding();
byte[] stringBytes = converter.GetBytes(inputString); 
//byte[]转string
System.Text.UnicodeEncoding converter = newSystem.Text.UnicodeEncoding();
stringoutputString = converter.GetString(stringByte); 
  
1.由string的rgb数值"255,255,0"转换为color

{
  string[]  color_params = e.Parameter.ToString().Split(',');
  byte color_R = Convert.ToByte(color_params[0]);
  byte color_G = Convert.ToByte(color_params[1]);
  byte color_B = Convert.ToByte(color_params[2]);
}

2.由颜色名称字符串("black") 转化为color

 {

  //ColorConverter c = new ColorConverter();
  //object obj = c.ConvertFrom();
  //Color color = (Color)obj;
  Color color = Color.FromRgb(color_R, color_G, color_B);  

}

3.将blend的 8位颜色值转为color

        /// 

        /// 将blend的8位颜色值转为color
        /// 
        /// 
        /// 
        public Color ToColor(string colorName)
        {
            if (colorName.StartsWith("#"))
                colorName = colorName.Replace("#", string.Empty);
            int v = int.Parse(colorName, System.Globalization.NumberStyles.HexNumber);
            return new Color()
            {
                A = Convert.ToByte((v >> 24) & 255),
                R = Convert.ToByte((v >> 16) & 255),
                G = Convert.ToByte((v >> 8) & 255),
                B = Convert.ToByte((v >> 0) & 255)
            };
        }

 

技术交流群:29609188


 

你可能感兴趣的:(C#)