文章目录
1.绘画的命名空间
2.像素和坐标系
像素:构成图像的最小单位
坐标系:
————————————————> x轴
|
|
|
|
|
\/ y轴
3.颜色
4.Graphics类的部分方法
5.画刷(Brush)
6.路径(Path)
GraphicsPath类的常用属性和方法:
7.OnPaint方法
程序刚开始运行时在窗体上绘图
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
/*Graphics g = CreateGraphics();
SolidBrush brush=new SolidBrush(Color.Green);
g.FillEllipse(brush, 50, 30, 200, 100);
g.Dispose();
brush.Dispose();*/
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g=e.Graphics;
Pen pen=new Pen(Color.Red, 2);
g.DrawEllipse(pen, 20, 30, 230, 80);
}
}
8.绘图程序
8.1源代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();//绘图在窗体上
Pen pen = new Pen(Color.Red, 5);
Point startPoint = new Point(50, 80);
Point endPoint = new Point(250, 30);
g.DrawLine(pen, startPoint, endPoint);
//释放资源
g.Dispose();
pen.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
Graphics g = CreateGraphics();
g.Clear(BackColor);
g.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//画椭圆
private void button3_Click(object sender, EventArgs e)
{
Graphics g = CreateGraphics();
g.FillEllipse(brush, 80, 30, 230, 100);//(80,30),(230,100)
g.Dispose();
}
private Brush brush = new SolidBrush(Color.Red);
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
brush = new SolidBrush(Color.Green);
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
brush = new TextureBrush(imageList1.Images[0]);
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
//渐变:从一种颜色到另一种颜色的变化
Point startPoint = new Point(80, 70);
Point endPoint = new Point(310, 70);
brush = new LinearGradientBrush(startPoint, endPoint, Color.Red, Color.Green);
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
brush = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Red, Color.RosyBrown);
}
//画五角星
private void button4_Click(object sender, EventArgs e)
{
Point[] pt = new Point[10];
pt[0] = new Point(120, 46);
pt[1] = new Point(156, 46);
pt[2] = new Point(168, 10);
pt[3] = new Point(180, 46);
pt[4] = new Point(214, 46);
pt[5] = new Point(188, 70);
pt[6] = new Point(198, 106);
pt[7] = new Point(168, 82);
pt[8] = new Point(138, 104);
pt[9] = new Point(150, 70);
GraphicsPath path = new GraphicsPath();
for (int i=0;i<8;i++)
{
path.AddLine(pt[i], pt[i+1]);
}
path.CloseFigure();
Graphics g = CreateGraphics();
g.FillPath(brush, path);
g.Dispose();
path.Dispose();
}
}
8.2代码解释
1. 构造函数和组件初始化
public Form1()
{
InitializeComponent();
}
这是Form1
的构造函数,它调用InitializeComponent()
方法来初始化窗体上的所有控件。
2. 绘制直线
private void button1_Click(object sender, EventArgs e)
{
// ... 绘图代码 ...
}
当用户点击button1
时,会在窗体上绘制一条红色的直线。这里使用了CreateGraphics()
方法来获取Graphics
对象,然后定义了直线的起点和终点,并使用Pen
对象绘制直线。最后,释放了Graphics
和Pen
对象占用的资源。
3. 清除窗体
private void button2_Click(object sender, EventArgs e)
{
Graphics g = CreateGraphics();
g.Clear(BackColor);
g.Dispose();
}
点击button2
时,会清除窗体上的所有绘图,使其背景色恢复为窗体的背景色。这里同样使用了CreateGraphics()
方法,并通过Clear
方法清除绘图。
4. 绘制椭圆
private void button3_Click(object sender, EventArgs e)
{
// ... 绘图代码 ...
}
点击button3
时,会在窗体上绘制一个红色的椭圆。这里定义了一个Brush
对象用于填充椭圆,并使用FillEllipse
方法绘制。
5. 更改填充样式
通过radioButton1
到radioButton4
的CheckedChanged
事件,用户可以更改用于绘图的填充样式。这些事件处理器分别设置了不同的Brush
对象,包括纯色、纹理、线性渐变和斜线图案。
6. 绘制五角星
private void button4_Click(object sender, EventArgs e)
{
// ... 定义点数组 ...
GraphicsPath path = new GraphicsPath();
// ... 添加线条到路径 ...
Graphics g = CreateGraphics();
g.FillPath(brush, path);
// 释放资源
}
点击button4
时,会在窗体上绘制一个五角星。这里首先定义了一个点数组来表示五角星的顶点,然后使用GraphicsPath
对象将这些点连接成形状,并通过FillPath
方法使用当前brush
填充该形状。