基本图形的绘制教学课件_第1页
基本图形的绘制教学课件_第2页
基本图形的绘制教学课件_第3页
基本图形的绘制教学课件_第4页
基本图形的绘制教学课件_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、回顾GDI+为开发者提供了一组实现与各种设备(例如监视器,打印机及其它具有图形化能力但不及涉及这些图形细节的设备)进行交互的库函数。 GDI+的本质在于,它能够替代开发人员实现与例如显示器及其它外设的交互;而从开发者角度来看,要实现与这些设备的直接交互却是一项艰巨的任务。第1页,共34页。目标掌握GDI+提供的函数和方法的使用讲解一个实例程序第2页,共34页。画点C#采用Point结构和SetPixel()方法完成画点的功能;其中Point用于图形设计,SetPixel()用于图像处理Point原型: public struct Point;使用: public Point p1 = new

2、Point();每个点结构有x和y两个属性,表示横纵坐标,如:p1.x = 30;p1.y = 100;第3页,共34页。1) DrawLine方法public void DrawLine( Pen pen, int x1, int y1,int x2, int y2 );或 public void DrawLine( Pen pen, Point pt1, Point pt2 );如:Graphics g = this.CreateGraphics( ); Pen p1 = new Pen( Color.Red, 2 ); Point pt1 = new Point( 40,50); Poi

3、nt pt2 = new Point( 220,150); g.DrawLine( p1, 10, 20, 40, 50 ); g.DrawLine( p1, pt1, pt2 );2) DrawLines方法public void DrawLines( Pen pen, Point pts );画直线第4页,共34页。private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Pen pen = new Pen(Color.Black, 3);Point points = new Point( 1

4、0, 10), new Point( 10, 100), new Point(200, 50), new Point(250, 120) ; e.Graphics.DrawLines(pen, points); 效果 画直线第5页,共34页。1) public void DrawEllipse(Pen pen, int x, int y, int width, int height) 其中x, y为椭圆外接矩形左上角的坐标,width定义椭圆的外接矩形的宽度,height定义椭圆外接矩形的高度。2) public void DrawEllipse(Pen pen, Rectangle rect

5、) 其中rect为Rectangle结构,用于确定椭圆的外接矩形。画椭圆第6页,共34页。public void DrawArc( Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle ) 其中x, y为椭圆外接矩形左上角的坐标,width定义椭圆。 startAngle圆弧起点, sweepAngle顺时针画过的角度的外接矩形的宽度,height定义椭圆外接矩形的高度。例: Graphics g = this.CreateGraphics( );Pen pen = new Pen(Color.

6、Red, 2 );g.Clear(this.BackColor);g.DrawArc(pen,0,0,200,300,-60,180);绘制圆弧第7页,共34页。public void DrawPie( Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle )例: Graphics g = this.CreateGraphics( );Pen pen = new Pen(Color.Red, 2 );g.Clear(this.BackColor);g.DrawPie(pen,60,60,160,

7、160,160,200);DrawPie(扇形)第8页,共34页。1) public void DrawRectangle(Pen pen, int x, int y, int width, int height)参数含意:2) public void DrawRectangle(Pen pen, Rectangle rect)参数含意:例:private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g = e.Graphics;Pen pen = new Pen( Color.B

8、lack, 3);Rectangle rect = new Rectangle( 30, 30, 200, 100);e.Graphics.DrawRectangle( pen, rect );画矩形第9页,共34页。3)public void DrawRectangles( Pen pen, Rectangle rects )该方法用于绘制多个矩形。例:private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g = e.Graphics;Pen pen = new Pen(C

9、olor.Black, 3);Rectangle rects = new Rectangle( 0, 0, 100, 200),new Rectangle(100, 200, 250, 50),new Rectangle(300, 0, 50, 100 ;e.Graphics.DrawRectangles( pen, rects ); 画矩形第10页,共34页。每段贝塞尔曲线都需要四个点,第一个点是起始点,第四个点是终止点,第二个点和第三个点控制曲线的形状。使用DrawBezier()方法绘制一段贝塞尔曲线,使用DrawBeziers()方法绘制多段贝塞尔曲线。常用形式有:1) public

10、void DrawBezier( Pen pen, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4 )2) public void DrawBezier( Pen pen, Point pt1, Point pt2, Point pt3, Point pt4 )3) public void DrawBeziers( Pen pen, Point points ) 其中points是Point结构的数组,第一段贝塞尔曲线从点数组中的第一个点到第四个点绘制而成。以后每段曲线只需要三个点:两

11、个控制点和一个结束点。前一段曲线的结束点会自动用作后一段曲线的起始点。 Bezier第11页,共34页。private void Form1_Paint(object sender, PaintEventArgs e)Pen blackPen = new Pen(Color.Black, 3);Point bezierPoints =new Point(50, 100),new Point(100, 10),new Point(150,290),new Point(200, 100),new Point(250,10),new Point(300, 290),new Point(350,100

12、) ; e.Graphics.DrawBeziers( blackPen, bezierPoints ); 示例第12页,共34页。public void DrawPolygon( Pen pen, Point points );public void DrawPolygon( Pen pen, PointF points );其中:PointF表示在二维平面中定义点的、浮点 x 和 y 坐标的有序对 例:画一个四边形private void button_Click(object sender, System.EventArgs e ) Graphics g = this.CreateGra

13、phics( ); Pen pen = new Pen( Color.Red, 2 ); g.Clear( this.BackColor ); Point p1 = new Point new Point( 10, 120 ), new Point( 120, 100), new Point( 300,180 ), new Point( 60, 200) ; g.DrawPolygon( pen, p1 );DrawPolygon(多边形)第13页,共34页。 这个方法用平滑的曲线将各节点连接起来,但会自动把首尾节点连接起来构成封闭曲线。public void DrawClosedCurve(

14、 Pen pen, Point pts );public void DrawClosedCurve( Pen pen, PointF pts );public void DrawClosedCurve( Pen, Point , float, FillMode );public void DrawClosedCurve( Pen, PointF , float, FillMode ); 其中float型参数指定弯曲强度,该值范围为0.0f 1.0f,超出此范围会产生异常,当弯曲强度为零时,就是直线,默认张力为0.5。示例:Pen blackPen = new Pen( Color.Black

15、);Point p1 = new Point new Point( 10, 120 ), new Point( 120, 100), new Point( 300,180 ), new Point( 60, 200) ;g.DrawClosedCurve( blackPen, p1 );DrawClosedCurve方法第14页,共34页。 DrawCurve方法(以四个点画出一条基本曲线)绘制经过一组指定的Point结构数组定义的曲线,最后一个点与第一个点间不画线。public void DrawCurve( Pen pen, Point pts );public void DrawCurv

16、e( Pen pen, PointF pts );public void DrawCurve( Pen, Point , float );public void DrawCurve( Pen, PointF , float );其中float参数代表曲线弯曲的强度。示例:private void button_Click(object sender, System.EventArgs e ) Graphics g = this.CreateGraphics( ); g.Clear( this.BackColor ); Pen blackPen = new Pen( Color.Black, 3

17、 ); Point p1 = new Point new Point( 10, 120 ), new Point( 120, 100), new Point( 300,180 ), new Point( 60, 200) ; g.DrawCurve( blackPen, p1 );DrawCurve方法第15页,共34页。private void Form1_Paint(object sender,PaintEventArgs e)Pen redPen = new Pen(Color.Red, 3);Pen greenPen = new Pen(Color.Green, 3);Point cu

18、rvePoints = new Point( 50, 250),new Point(100, 25),new Point(200, 250),new Point(250, 50),new Point(300, 75),new Point(350, 200),new Point(400, 150);e.Graphics.DrawLines(redPen, curvePoints);e.Graphics.DrawCurve(greenPen, curvePoints); 绘制直线与平滑曲线示例第16页,共34页。路径通过组合直线、矩形和简单的曲线形成的,可通过Graphics类DrawPath方法

19、来绘制整个路径的各个对象。public void DrawPath( Pen pen, GraphicsPath path );示例:private void button_Click(object sender,System.EventArgs e )Graphics g = this.CreateGraphics( );GraphicsPath graphPath = new GraphicsPath();graphPath.AddEllipse( 0, 0, 200, 100 );graphPath.AddRectangle( new Rectangle( 100, 80, 200, 1

20、00 );graphPath.AddBezier( 30, 60, 70, 60, 50, 30, 100, 10 );Pen blackPen = new Pen( Color.Black, 3 );g.DrawPath( blackPen, graphPath );DrawPath方法第17页,共34页。该方法用于画一个填充椭圆,常用格式有:public void FillEllipse( Brush brush, int x, int y, int width, int height );public void FillEllipse( Brush brush, RectangleF r

21、ect );示例:private void button_Click(object sender,System.EventArgs e )Graphics g = this.CreateGraphics( );g.Clear( this.BackColor );Brush sp = new SolidBrush( Color.Red );g.FillEllipse( sp, 80, 90, 200, 100 );FillEllipse方法第18页,共34页。该方法用于画一个填充矩形,常用格式有:public void FillRectangle( Brush brush, int x, int

22、 y, int width, int height );public void FillRectangle( Brush brush, Rectangle rect );示例:private void button_Click(object sender,System.EventArgs e )Graphics g = this.CreateGraphics( );g.Clear( this.BackColor );Brush sp = new SolidBrush( Color.Red );g.FillRectangle( sp, 80, 90, 200, 100 );FillRectang

23、le方法第19页,共34页。该方法用于画一个填充饼图,常用格式有:public void FillPie( Brush brush, int x, int y, int width, int height , int startAngle, int sweepAngle );2) public void FillPie( Brush brush, RectangleF rect, float startAngle, float sweepAngle );示例:private void button_Click(object sender,System.EventArgs e )Graphics

24、 g = this.CreateGraphics( );g.Clear( this.BackColor );Brush sp = new SolidBrush( Color.Red );g.FillPie( sp, 80, 90, 200, 100, -60, 300 );FillPie方法第20页,共34页。设计一个简单绘图板,功能类似Windows画图工具。功能有:能由鼠标控制绘制直线、矩形、椭圆,曲线,并能控制线条的颜色。 简单绘图板示例程序讲解第21页,共34页。程序界面作成在Form中拖入pictureBox和button控件,并进行合理布局。使界面如下:第22页,共34页。在“直线

25、”按钮Click事件中输入代码:Point p1 = new Point(10,10);Point p2 = new Point(50,50);Graphics g = this.pictureBox1.CreateGraphics();Pen pen = new Pen(Color.Black,1);g.DrawLine(pen,p1,p2);简单绘图板示例程序讲解第23页,共34页。 1.“直线”起点坐标获取:在控件pictureBox1中按下鼠标左键获取起点p1(X,Y), 在pictureBox1_MouseDown事件中输入代码:Point p1 = Point.Empty, p2

26、= Point.Empty;p1.X = e.X;p1.Y = e.Y;p2.X = 100;p2.Y = 100;Graphics g = this.pictureBox1.CreateGraphics();Pen pen = new Pen(Color.Black,1);g.DrawLine(pen,p1,p2);简单绘图板示例程序讲解第24页,共34页。 2.“直线”终点坐标获取:在控件pictureBox1中松开鼠标左键获取终点p2(X,Y), 在pictureBox1_MouseUp事件中输入代码:Point p1 = Point.Empty, p2 = Point.Empty;p1

27、.X = 100;p1.Y = 100;p2.X = e.X;p2.Y = e.Y;Graphics g = this.pictureBox1.CreateGraphics();Pen pen = new Pen(Color.Black,1);g.DrawLine(pen,p1,p2); 3. 对程序进行调整,使直线的起点和终点都从光标获取。private void pictureBox1_MouseDown() p1.X = e.X;p1.Y = e.Y;private void pictureBox1_MouseUp()p2.X = e.X;p2.Y = e.Y;Graphics g =

28、this.pictureBox1.CreateGraphics();Pen pen = new Pen(Color.Black,1);g.DrawLine(pen,p1,p2); 简单绘图板示例程序讲解第25页,共34页。 4.直线起点确定后,在pictureBox1控件中移动鼠标时,使直线可见, 在pictureBox1_MouseMove事件中输入代码:先设置全局bool变量MouseDown,表示鼠标左键是否按下。if(MouseDown) Graphics g = this.pictureBox1.CreateGraphics();Pen pen = new Pen(Color.Whi

29、te,1); g.DrawLine(pen,p1,p2); /清除前一根直线p2.X = e.X;p2.Y = e.Y;pen = new Pen(Color.Black,1);g.DrawLine(pen,p1,p2);g.Dispose(); 简单绘图板示例程序讲解第26页,共34页。 5. 移动鼠标时,为了原有直线不被清除, 在MouseMove事件重画原有的图形,那么必须先在MouseUp事件中保存每个已画的图形特征。 先设置全局动态数组和结构。 ArrayList addArray = new ArrayList(); public struct SharpType public string type; /图形形状public Point p1, p2;public Color foreColor; /画笔颜色 public SharpType(string type, Point p1, Point p2, Color foreColor) this.type = type; this.p1 = p1;this.p2 = p2; this.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论