C# 的Brush 及相关颜色的操作

              // (实心刷)
                  Rectangle rect1 = new Rectangle(20, 80, 250, 100);
                 SolidBrush sbrush1 = new SolidBrush(Color.DarkOrchid);                  
                 SolidBrush sbrush2 = new SolidBrush(Color.Aquamarine);      
                  SolidBrush sbrush3 = new SolidBrush(Color.DarkOrange);

         //(梯度刷)
                  LinearGradientBrush lbrush1 = new LinearGradientBrush(rect1,
                  Color.DarkOrange, Color.Aquamarine,
                  LinearGradientMode.BackwardDiagonal);

                 //(阴影刷)
                 HatchBrush hbrush1 = new HatchBrush(HatchStyle.DiagonalCross,
                  Color.DarkOrange, Color.Aquamarine);
                  HatchBrush hbrush2 = new HatchBrush(HatchStyle.DarkVertical,
                  Color.DarkOrange, Color.Aquamarine);
                  HatchBrush hbrush3 = new HatchBrush(HatchStyle.LargeConfetti,
                  Color.DarkOrange, Color.Aquamarine);

                 //(纹理刷)
                  textureBrush = new TextureBrush(new Bitmap(@"e:\123.jpg"));
               
   //e.Graphics.FillRectangle(hbrush1, rect1);
                  //e.Graphics.FillRectangle(sbrush1, rect1);

     
             //e.Graphics.FillRectangle(textureBrush, rect1);
                  e.Graphics.FillRectangle(lbrush1, rect1);

------------------------------------

using System.Windows.Media;

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;




    
    
    
    
    
    



    




你可能感兴趣的:(C# 的Brush 及相关颜色的操作)