C# Winform简单绘图 (在直角坐标系中画折线)

        private void Form1_Load(object sender, EventArgs e)

        {
            string[] month = new string[12] { "Jan ", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            float[] d = new float[12] { 20.5f, 60, 10.8f, 15.6f, 30, 70.9f, 50.3f, 30.7f, 70, 50.4f, 30.8f, 20 };
            //画图初始化
            Bitmap bitmap = new Bitmap(500, 500);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.WhiteSmoke);
            PointF cpt = new PointF(40, 420);//坐标中心点
            PointF[] xpt = new PointF[3] { new PointF(cpt.Y + 15, cpt.Y), new PointF(cpt.Y, cpt.Y - 8), new PointF(cpt.Y, cpt.Y + 8) };//x轴三角形
            PointF[] ypt = new PointF[3] { new PointF(cpt.X, cpt.X - 15), new PointF(cpt.X - 8, cpt.X), new PointF(cpt.X + 8, cpt.X) };//y轴三角形
            g.DrawString("One month production company a product chart", new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + 60, cpt.X));//图表标题
            //画x轴
            g.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.Y, cpt.Y);
            g.DrawPolygon(Pens.Black, xpt);//画空心的三角形,因下句代码是填充三角形,故可以省略
            g.FillPolygon(new SolidBrush(Color.Black), xpt);//填充实心三角形
            g.DrawString("Mounth", new Font("宋体", 12), Brushes.Black, new PointF(cpt.Y + 10, cpt.Y + 10));
            //画y轴
            g.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.X, cpt.X);
            g.DrawPolygon(Pens.Black, ypt);
            g.FillPolygon(new SolidBrush(Color.Black), ypt);
            g.DrawString("Units (million)", new Font("宋体", 12), Brushes.Black, new PointF(0, 7));
            for (int i = 1; i <= 12; i++)
            {
                //画y轴刻度
                if (i < 11)
                {
                    g.DrawString((i * 10).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X - 30, cpt.Y - i * 30 - 6));
                    g.DrawLine(Pens.Black, cpt.X - 3, cpt.Y - i * 30, cpt.X, cpt.Y - i * 30);
                }
                //画x轴项目
                g.DrawString(month[i - 1].Substring(0, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 5));//分割字符串,使“月份”纵向显示
                g.DrawString(month[i - 1].Substring(1, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 20));
                if (month[i - 1].Length > 2) g.DrawString(month[i - 1].Substring(2, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 35));
                //画点
                g.DrawEllipse(Pens.Black, cpt.X + i * 30 - 1.5f, cpt.Y - d[i - 1] * 3 - 1.5f, 3, 3);//画圆(因下填充,可省)
                g.FillEllipse(new SolidBrush(Color.Black), cpt.X + i * 30 - 1.5f, cpt.Y - d[i - 1] * 3 - 1.5f, 3, 3);//填充
                //画数值
                g.DrawString(d[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30, cpt.Y - d[i - 1] * 3));
                //画折线
                if (i > 1) g.DrawLine(Pens.Red, cpt.X + (i - 1) * 30, cpt.Y - d[i - 2] * 3, cpt.X + i * 30, cpt.Y - d[i - 1] * 3);
            }
            //保存输出图片
            //bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
            pictureBox1.Image = bitmap;

        }

        

在.net中,微软给我们提供了画图类(system.drawing.imaging),在该类中画图的基本功能都有。比如:直线、折线、矩形、多边形、椭圆形、扇形、曲线等等,因此一般的图形都可以直接通过代码画出来。

接下来介绍一些画图函数:

Bitmap bmap=new Bitmap(500,500)//定义图像大小;

bmap.Save(stream,imagecodecinfo) //将图像保存到指定的输出流;

Graphics g //定义或创建i绘图对像;

PointF cpt//定义二维平面中x,y坐标;

DrawString(string,font,brush,ponitf) //用指定的brushfont对像在指定的矩形或点绘制指定的字符串;

DrawLine(pen,ponit,ponit) //用指定的笔(pen)对像绘制指定两点之间直线;

DrawPolygon(pen,ponit[]) //用指定的笔(pen)对像绘制指定多边形,比如三角形,四边形等等;

FillPolygon(brush,ponit[]) //用指定的刷子(brush)对像填充指定的多边形;

DrawEllipse(pen,x,y,width,height) //用指定的笔绘制一个边框定义的椭圆;

FillEllipse(brush,x,y,width,height) //用指定的刷子填充一个边框定义的椭圆;

DrawRectangle(pen,x,y,width,height) //用指定的笔绘制一个指定坐标点、宽度、高度的矩形;

DrawPie(pen,x,y,width,height,startangle,sweepangle) //用指定的笔绘制一个指定坐标点、宽度、高度以及两条射线组成的扇形;

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值