四、使用GDI+画曲线
DrawBezier(Pen,Point,Point,Point,Point)
DrawBezier(Pen,PointF,PointF,PointF,PointF)
DrawBezier(Pen,Single,Single,Single,Single,Single,Single,Single,Single)
DrawCurve(Pen,Point[])
DrawCurve(Pen,PointF[])
DrawCurve(Pen,Point[],Single)
DrawCurve(Pen,PointF[],Int32,Int32)
DrawCurve(Pen,Point[],Int32,Int32,Single)
DrawCurve(Pen,PointF[],Int32,Int32,Single)
private void Form1_Paint(object sender, PaintEventArgs e) { //创建画板从Paint事件中的直接引用Graphics对象 Graphics graphics = e.Graphics; graphics.Clear(Color.Black); //定义画笔 Pen pen = new Pen(Color.White, 3.0f); Pen thickPen = new Pen(Color.White,2.0f); Pen thick = new Pen(Color.Red, 2.0f); //Rectangle是椭圆的一部分,Rectangle结构 //第一个-60是指与X轴的角度,顺时针,第二个弧线结束点到开始的角度 Point start = new Point(100, 100); Point control1 = new Point(200, 400); Point control2 = new Point(400, 400); Point end = new Point(500, 100); graphics.DrawBezier(pen, start, control1, control2, end); }
在上次坐标的基础上继续画曲线
private void Form1_Paint(object sender, PaintEventArgs e) { //创建画板从Paint事件中的直接引用Graphics对象 Graphics graphics = e.Graphics; graphics.Clear(Color.Black); //定义画笔 Pen pen = new Pen(Color.White, 3.0f); Pen thickPen = new Pen(Color.White, 2.0f); Pen thick = new Pen(Color.Red, 2.0f); //画横坐标 Point pointFirstX = new Point(0, this.ClientRectangle.Height / 2); Point pointFirstY = new Point(this.ClientRectangle.Width, this.ClientRectangle.Height / 2); Point pointSecondX = new Point(this.ClientRectangle.Width / 2, 0); Point pointSecondY = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height); graphics.DrawLine(pen, pointFirstX, pointFirstY); graphics.DrawLine(pen, pointSecondX, pointSecondY); //画箭头 Point PointUpLeftX = new Point(this.ClientRectangle.Width / 2, 0); Point PointUpLeftY = new Point(this.ClientRectangle.Width / 2 - 10, 10); Point PointUpRightY = new Point(this.ClientRectangle.Width / 2 + 10, 10); Point PointRightUpX = new Point(this.ClientRectangle.Width, this.ClientRectangle.Height / 2); Point PointRightUpY = new Point(this.ClientRectangle.Width - 10, this.ClientRectangle.Height / 2 - 10); Point PointRightDownY = new Point(this.ClientRectangle.Width - 10, this.ClientRectangle.Height / 2 + 10); graphics.DrawLine(pen, PointUpLeftX, PointUpLeftY); graphics.DrawLine(pen, PointUpLeftX, PointUpRightY); graphics.DrawLine(pen, PointRightUpX, PointRightUpY); graphics.DrawLine(pen, PointRightUpX, PointRightDownY); //画原点 PointF centre = new PointF(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2); Font font = new Font("黑体", 14.0f); Brush brush = Brushes.White; graphics.DrawString("0", font, brush, centre); //画坐标点 int iWidht = this.ClientRectangle.Width / 2; int iHeight = this.ClientRectangle.Height / 2; int j = 0; for (int i = iWidht; i < this.ClientRectangle.Width; i = i + 50) { Point tmpStart = new Point(i, iHeight); Point tmpEnd = new Point(i, iHeight - 8); graphics.DrawLine(thickPen, tmpStart, tmpEnd); if (j != 0) { graphics.DrawString(j.ToString(), font, brush, new Point(tmpEnd.X - 7, tmpEnd.Y + 10)); } j++; } int k = 0; for (int i = iWidht; i > 0; i = i - 50) { Point tmpStart = new Point(i, iHeight); Point tmpEnd = new Point(i, iHeight - 8); graphics.DrawLine(thickPen, tmpStart, tmpEnd); if (k != 0) { graphics.DrawString("-" + k.ToString(), font, brush, new Point(tmpEnd.X - 7, tmpEnd.Y + 10)); } k++; } int l = 0; for (int i = iHeight; i > 0; i = i - 50) { Point tmpStart = new Point(iWidht, i); Point tmpEnd = new Point(iWidht + 8, i); graphics.DrawLine(thickPen, tmpStart, tmpEnd); if (l != 0) { graphics.DrawString(l.ToString(), font, brush, new Point(tmpEnd.X, tmpEnd.Y - 9)); } l++; } int m = 0; for (int i = iHeight; i < this.ClientRectangle.Height; i = i + 50) { Point tmpStart = new Point(iWidht, i); Point tmpEnd = new Point(iWidht + 8, i); graphics.DrawLine(thickPen, tmpStart, tmpEnd); if (m != 0) { graphics.DrawString("-" + m.ToString(), font, brush, new Point(tmpEnd.X, tmpEnd.Y - 9)); } m++; } //定义坐标数组 PointF[] points = { new PointF(105.0f,102.0f), new PointF(106.0f,105.0f), new PointF(200.0f,121.0f), new PointF(220.0f,126.0f), new PointF(230.0f,110.0f), new PointF(300.0f,200.0f), new PointF(320.0f,95.0f), new PointF(343.0f,158.0f), new PointF(360.0f,100.0f), new PointF(450.0f,184.3f) }; //画折线 graphics.DrawLines(thick, points); //画曲线 Point beizerStart = new Point(200, 50); Point beizerControl1 = new Point(220, 287); Point beizerControl2 = new Point(460, 287); Point beizerEnd = new Point(500, 50); graphics.DrawBezier(thickPen, beizerStart, beizerControl1, beizerControl2, beizerEnd); }
使用DrawCurve绘制曲线
private void Form1_Paint(object sender, PaintEventArgs e) { //创建画板从Paint事件中的直接引用Graphics对象 Graphics graphics = e.Graphics; graphics.Clear(Color.Black); //定义画笔 Pen pen = new Pen(Color.White, 3.0f); Pen thickPen = new Pen(Color.White,2.0f); Pen thick = new Pen(Color.Red, 2.0f); //定义点坐标 Point[] points = { new Point(50,50), new Point(100,230), new Point(150,50) }; graphics.DrawCurve(pen, points); }
效果
使用它的重载DrawCurve(Pen,Point[],Single)画曲线
private void Form1_Paint(object sender, PaintEventArgs e) { //创建画板从Paint事件中的直接引用Graphics对象 Graphics graphics = e.Graphics; graphics.Clear(Color.Black); //定义画笔 Pen pen = new Pen(Color.White, 3.0f); Pen thickPen = new Pen(Color.White,2.0f); Pen thick = new Pen(Color.Red, 2.0f); //定义点坐标 Point[] points = { new Point(50,50), new Point(100,25), new Point(200,5), new Point(250,50), new Point(300,100), new Point(350,200), new Point(250,250) }; graphics.DrawCurve(pen, points,0.5f); }
第三个参数是张力(平滑度),如果等于0.0的话就是线段了,相当于DrawLines,越接近1.0越光滑。如果大于1.0的出现异常结果。
下面是从0.0,0.2,0.4,0.6,0.8,1.0的效果图