可以通过指定起始的颜色,和结束的颜色进行绘制渐变颜色,也可以给这个画刷指定一个颜色的数组,和颜色位置的数组。这个画刷就会根据这些数组来绘制渐变颜色。
下面是LinearGradientBrush实现的各作实例代码:
1,
Graphics g = e.Graphics; LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), new Point(10, 10), Color.Red, Color.Blue); g.FillRectangle(brush, 5, 5, 20, 20); brush.Dispose();
2,
Graphics g = this.CreateGraphics(); Rectangle rect = new Rectangle(50,50,100,100); LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Transparent, Color.Transparent, LinearGradientMode.Horizontal); Color[] colors = new Color[4]; colors[0] = Color.Blue; colors[1] = Color.Black; colors[2] =Color.Red; colors[3] =Color.Yellow; ColorBlend blend = new ColorBlend(); blend.Positions = new float[] { 0.0f, 0.5f,0.7f, 1.0f }; blend.Colors = colors; brush.InterpolationColors = blend; g.FillRectangle(brush, rect); brush.Dispose();
3,
LinearGradientBrush brush = new LinearGradientBrush(
this.ClientRectangle, Color.White,
Color.Red, LinearGradientMode.Horizontal);
Graphics g = this.CreateGraphics();
// g.FillRectangle(brush, 1, 1, 100, 100);
g.DrawString("Likecs.com", this.Font, brush, 100, 100);
brush.Dispose();
4, LinearGradientBrush brush = new LinearGradientBrush(
this.ClientRectangle, Color.White,
Color.Red, LinearGradientMode.Horizontal);
brush.SetBlendTriangularShape(0.5f); //中间向两边渐变效果
Graphics g = this.CreateGraphics();
g.FillRectangle(brush, 1, 1, 1000, 100);
g.DrawString("Likecs.com", this.Font, brush, 100, 100);
5, LinearGradientBrush brush = new LinearGradientBrush(
this.ClientRectangle, Color.White,
Color.Red, LinearGradientMode.Horizontal);
brush.SetSigmaBellShape(0.2f, 0.2f);//创建基于钟形曲线的渐变过渡过程。
Graphics g = this.CreateGraphics();
g.FillRectangle(brush, 1, 1, 1000, 100);
g.DrawString("Likecs.com", this.Font, brush, 100, 100);
(责任编辑:小白菜)
联系客服