原文:WPF Color、String、Brush转换

版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/BYH371256/article/details/79957419

//int 转 System.Windows.Media.Color
private System.Windows.Media.Color ToColor(int rgba)
{
return new System.Windows.Media.Color()
{
    A = Convert.ToByte((rgba >> 24) & 255),
    R = Convert.ToByte((rgba >> 16) & 255),
    G = Convert.ToByte((rgba >> 8) & 255),
    B = Convert.ToByte((rgba >> 0) & 255)
};
} //作用域:using System.Windows.Media; //由整形(int)转成Color的Argb值  A:透明值,R:红色值, G:绿色值, B:蓝色值
public static Color RgbToColor(int color)
{
    return Color.FromArgb((byte)0xff, (byte)((color & 0xff0000) >> 16), (byte)((color & 0x00ff00) >> 8), (byte)(color & 0x0000ff));
} //整形ARGB值转为Color值 Color cFrontColor = Color.FromArgb((byte)FrontColorA, (byte)style.nFrontColor_R, (byte)style.nFrontColor_G, (byte)style.nFrontColor_B); //Color 值转为整形(int)
public int ToArgb(Color color)
{
    int argb = color.A << 24;
    argb += color.R << 16;
    argb += color.G << 8;
    argb += color.B;
    return argb;
} //Color 值转R、G、B值
int fcolorR = (byte)((myRollText.FrontColor & 0xff0000) >> 16);
int fcolorG = (byte)((myRollText.FrontColor & 0x00ff00) >> 8);
int fcolorB = (byte)(myRollText.FrontColor & 0x0000ff); //整形(int)值转 SolidColorBrush类型
Color cForeBrush = RgbToColor(myRollText.FrontColor);
rollvm.RollShowVM.ForeBrush = new SolidColorBrush(cForeBrush); //Brush 转 Color            System.Windows.Media.Brush 转S ystem.Windows.Media.Color
//先将Brush转成string,再转成Color
Brush brush = ((Border)e.Source).Background;
Color c = (Color)ColorConverter.ConvertFromString(brush.ToString()); //将Brush转成SolidColorBrush,再取Color
Color color= ((SolidColorBrush)CadColor.Background).Color; //Color 转 Brush
Brush brush = new SolidColorBrush(color); //Brush 转 SolidColorBrush
SolidColorBrush ForeBrush = (SolidColorBrush)ci.ExSelectedBrush; //String 转换成 Color
Color color = (Color)ColorConverter.ConvertFromString(string); //String 转换成 Brush
BrushConverter brushConverter = new BrushConverter();
Brush brush = (Brush)brushConverter.ConvertFromString(string); //System.Windows.Media.Color 转 System.Windows.Media.Brush Color fcolor = (Color)ColorConverter.ConvertFromString(wndset.FonzColor.ToString());
var ForeColor = System.Drawing.Color.FromArgb(fcolor.A, fcolor.R, fcolor.G, fcolor.B); //System.Windows.Media.Brush 赋初值    
Brush forColor = new SolidColorBrush(System.Windows.Media.Color.FromArgb((byte)255, (byte)0,(byte)255,(byte)0));

05-11 15:53