欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > zy78_C#窗体绘图

zy78_C#窗体绘图

2026/4/30 12:03:38 来源:https://blog.csdn.net/weixin_47149785/article/details/142467493  浏览:    关键词:zy78_C#窗体绘图

文章目录

  • 1.绘画的命名空间
  • 2.像素和坐标系
  • 3.颜色
  • 4.Graphics类的部分方法
  • 5.画刷(Brush)
  • 6.路径(Path)
  • 7.OnPaint方法
  • 8.绘图程序
    • 8.1源代码:
    • 8.2代码解释
      • 1. 构造函数和组件初始化
      • 2. 绘制直线
      • 3. 清除窗体
      • 4. 绘制椭圆
      • 5. 更改填充样式
      • 6. 绘制五角星

1.绘画的命名空间

绘图空间

2.像素和坐标系

像素:构成图像的最小单位

坐标系:

————————————————> x轴

|

|

|

|

|

\/ y轴

3.颜色

在这里插入图片描述
在这里插入图片描述

4.Graphics类的部分方法

Graphics类的部分方法

5.画刷(Brush)

画刷

6.路径(Path)

路径

GraphicsPath类的常用属性和方法:

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对象绘制直线。最后,释放了GraphicsPen对象占用的资源。

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. 更改填充样式

通过radioButton1radioButton4CheckedChanged事件,用户可以更改用于绘图的填充样式。这些事件处理器分别设置了不同的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填充该形状。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词