using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DrawEx { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; } private void Form1_Load(object sender, EventArgs e) { string[] month = new string[] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }; float[] data = new float[] { 20.5F, 60, 10.8F, 15.6F, 30, 110.9F, 96.3F, 30.7F, 70, 50.4F, 30.8F, 20 }; Bitmap bMap = new Bitmap(500, 500); Graphics gph = Graphics.FromImage(bMap); gph.Clear(Color.Gray); PointF cPt = new PointF(40, 460); PointF xAxisEndPoint = new PointF(cPt.X + 420, cPt.Y); PointF yAxisEndPoint = new PointF(cPt.X, cPt.Y - 420); PointF[] xPt = new PointF[3] { new PointF(xAxisEndPoint.X + 15, xAxisEndPoint.Y), new PointF(xAxisEndPoint.X, xAxisEndPoint.Y - 5), new PointF(xAxisEndPoint.X, xAxisEndPoint.Y + 5) }; PointF[] yPt = new PointF[3] { new PointF(yAxisEndPoint.X, yAxisEndPoint.Y - 15), new PointF(yAxisEndPoint.X - 3, yAxisEndPoint.Y), new PointF(yAxisEndPoint.X + 3, yAxisEndPoint.Y) }; gph.DrawString("绘制折线图练习", new Font("宋体", 14), Brushes.Black, new PointF(cPt.X + 150, cPt.Y - 450)); gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.X + 420, cPt.Y); gph.DrawLine(Pens.Black, cPt, new PointF(cPt.X, cPt.Y - 420)); gph.DrawPolygon(Pens.Black, xPt); // 画轴三角形 gph.FillPolygon(new SolidBrush(Color.Black), xPt); gph.DrawPolygon(Pens.Black, yPt); // 画轴三角形 gph.FillPolygon(new SolidBrush(Color.Black), yPt); gph.DrawString("单位(万)", new Font("宋体", 12), Brushes.Black, new PointF(yPt[0].X - 26, yPt[0].Y - 18)); gph.DrawString("月份", new Font("宋体", 12), Brushes.Black, new PointF(xPt[0].X - 26, xPt[0].Y + 18)); PointF point1; PointF point2 = cPt; for (int i = 1; i <= month.Length; i++) { // 画Y轴刻度和标签 gph.DrawLine(Pens.Black, cPt.X, cPt.Y - i * 35, cPt.X - 5, cPt.Y - i * 35); gph.DrawString((i * 10).ToString(), new Font("宋体", 12), Brushes.Black, new PointF(cPt.X - 30, cPt.Y - i * 35 - 6)); // 画X轴的刻度和标签 gph.DrawLine(Pens.Black, cPt.X + i * 32, cPt.Y, cPt.X + i * 32, cPt.Y + 5); if (i < 11) gph.DrawString(month[i - 1].ToString(), new Font("宋体", 10), Brushes.Black, new PointF(cPt.X + 32 * i - 20, cPt.Y + 15)); else gph.DrawString(month[i - 1].ToString(), new Font("宋体", 8), Brushes.Black, new PointF(cPt.X + 32 * i - 12, cPt.Y + 15)); point1 = new PointF(cPt.X + i * 32, cPt.Y - data[i - 1] * 3.5f); // 画折线 if (i > 1) { gph.DrawLine(Pens.Red, point1, point2); } //画没点的数值标签 gph.DrawString(data[i - 1].ToString(), new Font("宋体", 11), Brushes.Blue, new PointF(point1.X + 1, point1.Y + 3)); // 数值原点 gph.FillEllipse(new SolidBrush(Color.Black), point1.X, point1.Y, 5, 5); point2 = point1; } pictureBox1.Image = bMap; } } }
pictureBox1
的Anchor
属性设置了:Top,Bottom,Left,Right
。整体思路是,先画在一个图片上面,然后再把图片加载到pictureBox1
里面去,这样不会动下窗口就消失。
主要用了Graphics
类里面的DrawString
,DrawLine
,DrawPolygon
,FillPolygon
等函数。
参考:
《C#入门到精通》