WPF color、brush、string转换

时间:2025-03-02 07:21:44
//color转为brush:
Brush br = new SolidColorBrush((0,0,0)); 
//string转Color
(Color)((string)str);
//Color转string((Color)value).ToString();
string和Brush的转换
Brush color = newSolidColorBrush((Color)((string)str)); 
//Brush转string
((Brush)value).ToString();
//string转byte[]
 converter = ();
byte[] stringBytes = (inputString); 
//byte[]转string
 converter = ();
stringoutputString = (stringByte); 
  
1.由string的rgb数值"255,255,0"转换为color

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

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

 {

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

}

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

        /// <summary>

        /// 将blend的8位颜色值转为color
        /// </summary>
        /// <param name="colorName"></param>
        /// <returns></returns>
        public Color ToColor(string colorName)
        {
            if (("#"))
                colorName = ("#", );
            int v = (colorName, );
            return new Color()
            {
                A = ((v >> 24) & 255),
                R = ((v >> 16) & 255),
                G = ((v >> 8) & 255),
                B = ((v >> 0) & 255)
            };
        }

 

技术交流群:29609188