渐变颜色LinearGradientBrush
LinearGradientBrush是用来渐变效果的,使用的初始化参数是矩形区域,开始颜色,结束颜色,颜色变化方向,它能够形成比较统一的渐变效果,对比PathGradientBrush的渐变,它的特点是它与周围的颜色渐变频率是一样的,设置LinearGradientBrush的区域大小时,可以想象一个大的有渐变色的长方形,压在多边形上面,并根据多边形的边界裁剪。
效果图
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Graphic
{
public partial class Demo_UseLinearGradientBrush : Form
{
public Demo_UseLinearGradientBrush()
{
InitializeComponent();
}
private void b_start_color_Click(object sender, EventArgs e)
{
ColorDialog clr = new ColorDialog();
clr.ShowDialog();
this.p_start_color.BackColor = clr.Color;
}
private void b_end_color_Click(object sender, EventArgs e)
{
ColorDialog clr = new ColorDialog();
clr.ShowDialog();
this.p_end_color.BackColor = clr.Color;
}
private void p_drawpic_Paint(object sender, PaintEventArgs e)
{
drawpic(this.p_start_color.BackColor, this.p_end_color.BackColor);
}
//绘图方法
private void drawpic(Color start, Color end)
{
LinearGradientBrush br =
new LinearGradientBrush(this.ClientRectangle, start, end, LinearGradientMode.ForwardDiagonal);
Graphics g = this.p_drawpic.CreateGraphics();//为控件创建Graphics
g.Clear(this.BackColor);
g.FillRectangle(br,this.ClientRectangle);
Font font = new Font("隶书", 50, FontStyle.Bold);
g.DrawString("测试", font, br, new PointF(100, 50));
g.Dispose();
}
}
}