C#——GDI简单绘图
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics(); //绘图用到的类
Pen pen = new Pen(Brushes.Red); //画笔
Point p1 = new Point(30, 50); //坐标点
Point p2 = new Point(250, 250); //坐标点
g.DrawLine(pen, p1, p2); //两点连成线
}
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics(); //绘图用到的类
Pen pen = new Pen(Brushes.Red); //画笔
Point p1 = new Point(30, 50); //坐标点
Point p2 = new Point(250, 250); //坐标点
g.DrawLine(pen, p1, p2); //两点连成线
}
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Brushes.Red);
Size size = new Size(100, 100); //大小
Rectangle rec = new Rectangle(new Point(50, 50), size); //一个起始点,大小表示长和宽
g.DrawRectangle(pen, rec); //画矩形
// SolidBrush brush = new SolidBrush(Color.Blue);
//g.FillRectangle(brush, rec);
LinearGradientBrush lbush = new LinearGradientBrush(rec,Color.Red,Color.Yellow,LinearGradientMode.BackwardDiagonal); //渐变色
g.FillRectangle(lbush,rec); //填充渐变色到矩形里
}
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Brushes.Red);
Size size = new Size(100, 100); //大小
Rectangle rec = new Rectangle(new Point(50, 50), size); //一个起始点,大小表示长和宽
g.DrawRectangle(pen, rec); //画矩形
// SolidBrush brush = new SolidBrush(Color.Blue);
//g.FillRectangle(brush, rec);
LinearGradientBrush lbush = new LinearGradientBrush(rec,Color.Red,Color.Yellow,LinearGradientMode.BackwardDiagonal); //渐变色
g.FillRectangle(lbush,rec); //填充渐变色到矩形里
}
private void button2_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Brushes.Black);
Size size = new Size(180, 180);
Rectangle rec = new Rectangle(new Point(200, 200), size);
g.DrawPie(pen, rec, 90, 90); //后面两个参数表示角度,起始角和终角,这个画出来是一个四分之一的圆
LinearGradientBrush lbush = new LinearGradientBrush(rec, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
g.FillPie(lbush,rec,90,90);
}
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Brushes.Black);
Size size = new Size(180, 180);
Rectangle rec = new Rectangle(new Point(200, 200), size);
g.DrawPie(pen, rec, 90, 90); //后面两个参数表示角度,起始角和终角,这个画出来是一个四分之一的圆
LinearGradientBrush lbush = new LinearGradientBrush(rec, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
g.FillPie(lbush,rec,90,90);
}
private void button3_Click(object sender, EventArgs e)
{
Size size = new Size(180, 180);
Rectangle rec = new Rectangle(new Point(400, 400), size);
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue,100);
g.DrawEllipse(pen,rec);
LinearGradientBrush lbush = new LinearGradientBrush(rec, Color.White, Color.Blue, LinearGradientMode.BackwardDiagonal);
g.FillEllipse(lbush, rec);
}
}
{
Size size = new Size(180, 180);
Rectangle rec = new Rectangle(new Point(400, 400), size);
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue,100);
g.DrawEllipse(pen,rec);
LinearGradientBrush lbush = new LinearGradientBrush(rec, Color.White, Color.Blue, LinearGradientMode.BackwardDiagonal);
g.FillEllipse(lbush, rec);
}
}