分享

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

 贾朋亮博客 2011-06-21
              // (实心刷)
                  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;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多