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));
自己的:
获取颜色对话框中选择的颜色
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Color col = new Color();
if (this.colorDialog1.ShowDialog() == DialogResult.OK)
col= colorDialog1.Color;
SolidBrush colorBrush = new SolidBrush(col ); //获取鼠标点下的位置
Point p = new Point(e.X, e.Y);
//判断鼠标点下的位置是否包含在矩形里面,以此判断是否选中某个矩形
if (rect.Contains(p))
{
Graphics g = pictureBox1.CreateGraphics();
g.FillRectangle(colorBrush , rect);//填充颜色
g.Dispose();
}
}
4、Brush转换成Color有两种方法:
(1)先将Brush转成string,再转成Color。
Color color= (Color)ColorConverter.ConvertFromString(brush.ToString());
(2)将Brush转成SolidColorBrush,再取Color。
Color color= ((SolidColorBrush)CadColor.Background).Color;