C#绘制直线,多边形、矩形、椭圆、曲线、圆弧、扇形。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 绘制一条直线
            Graphics graphics = this.CreateGraphics();//为当前窗体创建Graphics对象
            Pen pen1 = new Pen(Color.Red, 6);//实例化一个Pen对象,设定颜色为红色,线条宽度为6
            Point startpoint = new Point(20, 320);//设定起点坐标
            Point endtpoint = new Point(60, 120);//设定终点坐标
            graphics.DrawLine(pen1, startpoint, endtpoint);//调用方法开始绘制一条直线
            //graphics.DrawLine(pen1, 20, 320, 60, 120);
            //绘制折线
            Pen pen2 = new Pen(Color.Blue,3);
            Point[] point1 = {
                new Point(15,20),
                new Point(15,90),
                new Point(80,90),
                new Point(99,20)
        };
            graphics.DrawLines(pen2, point1);//调用方法开始绘制多条直线

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //绘制多边形
            Graphics graphics = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Yellow, 2);
            Point[] point1 = {
                new Point(12,90),
                new Point(60,80),
                new Point(90,20),
                new  Point(78,150)
            };
            graphics.DrawPolygon(pen1, point1);//调用DrawPolygon()方法绘制一个空心多变形
            Brush brush = new SolidBrush(Color.Black);//实例化一个画刷
            Point[] point2 = {
               new Point(12,90),
                new Point(60,80),
                new Point(90,20),
                new  Point(78,150)

            };
            graphics.FillPolygon(brush, point2);//调用FillPolygon()方法绘制一个实心多变形
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //绘制矩形
            Graphics graphics = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Green, 6);
            Brush brush = new SolidBrush(Color.Red);
            Rectangle[] rectangle2 = {
               new Rectangle(new Point(99, 60), new Size(120, 80)),
               new Rectangle(90,120,120,160),
               new Rectangle(152,155,120,160)

        };
            Rectangle rectangle1 = new Rectangle(new Point(10, 60), new Size(60, 80));
            graphics.DrawRectangle(pen1, rectangle1);//调用DrawRectangle()方法绘制空心矩形
            graphics.DrawRectangle(pen1, 90, 20, 88, 150);
            graphics.DrawRectangles(pen1, rectangle2);//调用DrawRectangles()方法绘制多个空心矩形
            graphics.FillRectangle(brush, rectangle1);//调用FillRectangle方法绘制一个实心矩形
            graphics.FillRectangles(brush, rectangle2); //调用FillRectangles方法绘制多个实心矩形

        }

        private void button4_Click(object sender, EventArgs e)
        {
            //绘制椭圆
            Graphics graphics = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Blue);
            Rectangle[] rect = {
            new Rectangle(188,98,123,188),
             new Rectangle(170,110,123,188)
            };
            Rectangle rectangle1 = new Rectangle(new Point(188, 120), new Size(120, 90));
            Brush brush1 = new SolidBrush(Color.Green);
            for(int i=0;i<=30;i++)
            graphics.DrawEllipse(pen1, 90*i+10, 110 * i + 10,188,130);//通过循环绘制多个空心的椭圆
            for (int i = 0; i <= 30; i++)
                graphics.FillEllipse(brush1, 90 * i + 10, 110 * i + 10, 188, 130);//通过循环绘制多个实心的椭圆
            graphics.DrawEllipse(pen1, rectangle1);//绘制一个空心的椭圆
            graphics.FillEllipse(brush1, rectangle1);//绘制一个实心椭圆
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //绘制曲线
            Graphics graphics = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Brown,3);
            Point[] point = {
                new Point(20,140),
                new Point(60,10),
                new Point(100,130),
                new Point(140,20),
                new Point(180,130),
                new Point(220,30),
                new Point(260,120)
            };
            graphics.DrawCurve(pen1, point, 1.0f);//绘制开口曲线
            graphics.DrawClosedCurve(pen1, point);//绘制封闭曲线
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //绘制圆弧
            Graphics graphics = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Green, 2);
            Rectangle rectangle = new Rectangle(40, 20, 200, 160);
            graphics.DrawArc(pen1, rectangle, 210, 90);//绘制圆弧
            graphics.DrawArc(pen1, 60,200,180,200, 210, 90);//绘制圆弧
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //绘制扇形
            Graphics graphics = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Green, 2);
            Brush brush = new SolidBrush(Color.Red);
            Rectangle rectangle = new Rectangle(40, 20, 200, 160);
            graphics.DrawPie(pen1, rectangle, 210, 90);
            graphics.DrawPie(pen1, 60, 200, 180, 200, 210, 90);//绘制一个空心扇形
                graphics.FillPie(brush, rectangle, 210, 90);
            graphics.FillPie(brush, 60, 200, 180, 200, 210, 90);//绘制一个实心扇形
        }
    }
}

  • 7
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gxbfeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值