收藏 分享(赏)

C#面向对象程序设计及实践教程PPT第九章.pptx

上传人:dzzj200808 文档编号:3315520 上传时间:2018-10-12 格式:PPTX 页数:38 大小:125.41KB
下载 相关 举报
C#面向对象程序设计及实践教程PPT第九章.pptx_第1页
第1页 / 共38页
C#面向对象程序设计及实践教程PPT第九章.pptx_第2页
第2页 / 共38页
C#面向对象程序设计及实践教程PPT第九章.pptx_第3页
第3页 / 共38页
C#面向对象程序设计及实践教程PPT第九章.pptx_第4页
第4页 / 共38页
C#面向对象程序设计及实践教程PPT第九章.pptx_第5页
第5页 / 共38页
点击查看更多>>
资源描述

1、第九章 图形图像编程,主要内容:,9.1 GDI+概述 9.2 Graphics类 9.3 基本图形的绘制和填充 9.4 常用画刷的创建及使用 9.5 绘制文本 9.6 Bitmap类 9.7 图像的处理 9.8 案例,【教学目标】, 掌握使用Graphics类绘制图形的基本步骤 了解常用的绘图对象 掌握C#中基本图形的绘制方法 了解画刷的创建及使用 了解绘制文本的方法 了解Bitmap类的常用属性和方法 掌握常用的图像处理方法,9.1 GDI+概述,GDI+(Graphice Device Interface Plus,图形设备接口)提供了各种丰富的图形图像处理功能 在C GDI+由很多类和

2、结构组成,使用GDI+编写图形图像程序时,需要加入命名空间System.Drawing。,9.2 Graphics类,Graphics类是一个密封类,不能有派生类。9.2.1 使用Graphics类绘图的基本步骤使用Graphics类绘图的基本步骤: (1) 创建Graphics对象 (2) 创建绘图工具 (3) 使用Graphics对象的方法绘图、显示文本或处理图像,【例9-1】 在窗体上用红色的画笔绘制一个矩形。private void Form1_Paint(object sender, PaintEventArgs e) Graphics g = e.Graphics; /定义Grap

3、hics对象gPen myPen = new Pen(Color.Blue); /定义蓝色画笔对象myPeng.DrawRectangle(myPen, 80, 80, 200, 100); /绘制矩形 ,9.2.2窗体的Paint事件,Windows应用程序的窗体和控件都有Paint事件。当窗体或控件上需要重新绘图时就会触发Paint事件。如果在控件的Paint事件中利用传递的参数获取Graphics对象,则绘制的图形图像仅在该控件内显示。如果在窗体的Paint事件中绘制,则绘制的图形图像在该窗体内显示。Form类窗体不能自动响应Paint事件,编程者必须将需要重新绘制图形的程序代码放置在窗

4、体的Paint事件中,这样,才能实现窗体的重绘。,9.2.3常用绘图对象,1. Point结构 Point结构表示在二维平面中定义点的整数x和y坐标的有序对。 2. Size结构 Size结构用来存储一个有序整数对,通常为矩形的宽度和高度。Size结构中也有两个整数Width和Height,表示水平和垂直距离。 3. Rectangle结构 存储一组整数,共四个,表示一个矩形的位置和大小。 4. Color结构 使用Color结构可以定义颜色。任何一种颜色都由透明度(A)和红绿蓝三基色(R,G,B)组成。 5. Pen类,【例9-3】 在窗体上,定义画笔,并设置其属性。,private voi

5、d Form1_Paint(object sender, PaintEventArgs e) Graphics g = this.CreateGraphics(); Pen myPen = new Pen(Color.Red,5); /定义画笔对象myPen myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot; /设置画笔的虚线样式为划线点图案 myPen.StartCap = System.Drawing.Drawing2D.LineCap.SquareAnchor; /设置线条的起点使用帽线为方锚头帽 myPen.End

6、Cap = System.Drawing.Drawing2D.LineCap.ArrowAnchor; /设置线条的终点使用帽线为箭头状锚头帽 g.DrawLine(myPen, 50, 50, 200, 200);,9.3 基本图形的绘制和填充,9.3.1 绘制直线 void DrawLine(Pen pen,int x1,int y1,int x2,int y2); void DrawLine(Pen pen,Point p1, Point p2); void DrawLines(Pen pen,Point point); 【例9-4】 在窗体上,绘制两条直线。 Graphics g =

7、e.Graphics; Pen myPen = new Pen(Color.Blue, 2); Point p1=new Point (100,100); Point p2=new Point (200,200); g.DrawLine (myPen ,70,70,220,70); g.DrawLine(myPen, p1, p2); Point points = new Point(10, 10), new Point(40, 40), new Point(40, 200), new Point(220, 230) ; g.DrawLines(myPen, points);,9.3.2 绘制

8、矩形,绘制直线的常用方法: DrawRectangle (Pen pen, Rectangle rect DrawRectangle (Pen pen, int x, int y, int width, int height); DrawRectangles( Pen pen, Rectangle rects ); 可以使用Graphics对象提供的FillRectangle方法绘制一个填充矩形。 FillRectangle( Brush brush, int x, int y, int width, int height FillRectangle( Brush brush, Rectang

9、le rect ); FillRectangles( Brush brush, Rectangle rect );,【例9-5】 在窗体上绘制和填充矩形。,private void Form1_Paint(object sender, PaintEventArgs e) Graphics g = this.CreateGraphics();Pen myPen = new Pen(Color.Green, 3);Rectangle rect = new Rectangle(50, 50, 200, 150);Brush myBrush = new SolidBrush(Color.Pink);g

10、.DrawRectangle(myPen, rect);g.FillRectangle(myBrush, 100, 100, 130, 90); ,9.3.3 绘制多边形,Graphics对象使用DrawPolygon方法绘制多边形的轮廓,使用FillPolygon方法绘制填充多边形。 DrawPolygon方法和FillPolygon方法常用形式如下: DrawPolygon (Pen pen, Point points) FillPolygon (Brush brush, Point points,【例9-6】 在窗体上绘制和填充多边形。,private void Form1_Paint(

11、object sender, PaintEventArgs e)Graphics g = this.CreateGraphics();Pen myPen=new Pen (Color.Red,2); Point points1=new Point(20,20),new Point(100,60),new Point(150,150),new Point(10,150);g.DrawPolygon(myPen, points1);Brush myBrush = new SolidBrush(Color.Blue);Point points2 = new Point(170, 20), new P

12、oint(230, 20), new Point(270, 100), new Point(230, 200), new Point(170,200);g.FillPolygon(myBrush, points2);,9.3.4 绘制圆和椭圆,Graphics对象使用DrawEllipse方法绘制圆和椭圆,使用FillEllipse方法绘制填充圆和椭圆。DrawEllipse方法用于绘制圆和椭圆,常用的形式如下所示: DrawEllipse(Pen pen, int x, int y, int width, int height) DrawEllipse(Pen pen, Rectangle

13、rect)FillEllipse方法用于绘制填充圆和椭圆,常用的格式有: FillEllipse( Brush brush, int x, int y, int width, int height ) FillEllipse( Brush brush, Rectangle rect),【例9-7】 在窗体上,绘制和填充圆和椭圆。,private void Form1_Paint(object sender, PaintEventArgs e) Graphics g = this.CreateGraphics();Pen myPen = new Pen(Color.Red, 2);Brush m

14、yBrush=new SolidBrush (Color.Blue ); /定义画刷 Rectangle myRect1 = new Rectangle(50, 50, 300, 200); /定义矩形Rectangle myRect2 = new Rectangle(80, 80, 150, 150); /定义正方形g.DrawEllipse(myPen, myRect1); /通过定义的外接矩形绘制椭圆g.FillEllipse(myBrush, myRect2); /通过定义的外接正方形绘制填充圆 ,9.3.5 绘制弧线,Graphics对象使用DrawArc方法绘制圆弧。 DrawAr

15、c方法的常用结构如下所示: DrawArc( Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle) 【例9-8】 在窗体上,绘制一段圆弧。 private void Form1_Paint(object sender, PaintEventArgs e) Graphics g = this.CreateGraphics();Pen myPen = new Pen(Color.Red, 2);g.DrawArc(myPen, 30, 30, 300, 200, 90, 120);g.DrawAr

16、c(myPen, 30, 30, 300, 200, -60, 90); ,9.3.6 绘制扇形,Graphics对象使用DrawPie方法绘制空心的扇形,使用FillPie方法绘制填充扇形。 绘制空心扇形的DrawPie方法的常用形式有: DrawPie(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle) DrawPie(Pen pen, Rectangle rect, float startAngle, float sweepAngle)绘制填充扇形的FillPie方法的常用形式有: F

17、illPie(Brush brush, int x, int y, int width, int height, int startAngle, int sweepAngle) FillPie(Brush brush, Rectangle rect, int startAngle, int sweepAngle),【例9-9】 在窗体上,绘制和填充扇形。,private void Form1_Paint(object sender, PaintEventArgs e) Graphics g = this.CreateGraphics();Pen myPen = new Pen(Color.Re

18、d, 2);Brush myBrush=new SolidBrush (Color .Gold );Rectangle rect=new Rectangle (20,20,300,200);g.DrawPie(myPen, rect, 120, 90);g.FillPie(myBrush, 80, 50, 200, 200, -60, 60); ,9.3.7 绘制曲线,Graphics对象使用DrawCurve方法绘制非闭合曲线,使用DrawClosedCurve方法绘制闭合曲线。DrawCurve方法,常用形式有: DrawCurve (Pen pen ,Point points ); Dr

19、awCurve (Pen pen ,Point points,float tension ); DrawClosedCurve方法绘制闭合曲线: DrawClosedCurve (Pen pen ,Point points ); FillClosedCurve (Brush brush ,Point points);,【例9-10】 在窗体上,绘制和填充曲线。,private void Form1_Paint(object sender, PaintEventArgs e)Graphics g = this.CreateGraphics();Pen myPen = new Pen(Color.

20、Red, 2);Brush myBrush = new SolidBrush(Color.Blue);Point points1=new Point(20,30),new Point(50,60),new Point(90,30),new Point(150,100);Point points2 =new Point(50, 200),new Point(100,70),new Point(120,200),new Point(200,100),new Point(250,50);g.DrawCurve(myPen, points1);g.FillClosedCurve(myBrush, po

21、ints2);,9.4 常用画刷的创建及使用,SolidBrush画刷在命名空间System.Drawing中定义 TextureBrush、LinearGradientBrush、PathGradientBrush、HatchBrush画刷类使用时要加入命名空间System.Drawing.Drawing2D,9.4 常用画刷的创建及使用,9.4.1 SolidBrush单色画刷 例如: Brush myBrush=new SolidBrush (Color.Red); SolidBrush brush1 = new SolidBrush(Coloe.Blue);9.4.2 HatchBru

22、sh 阴影画刷 HatchBrush类的构造函数有两种形式,如下所示: HatchBrush(HatchStyle hatchStyle, Color foreColor); HatchBrush(HatchStyle hatchStyle, Color foreColor,Color backColor);,【例9-11】 在窗体上,使用不同的阴影画刷填充图形。,private void Form1_Paint(object sender, PaintEventArgs e)Graphics g=this.CreateGraphics ();HatchBrush brush1=new Hat

23、chBrush(HatchStyle.DarkHorizontal ,Color .Red );/椭圆填充为横线g.FillEllipse (brush1 ,20,20,100,60);HatchBrush brush2 = new HatchBrush(HatchStyle.Cross, Color.Green, Color.Gold);/正方形填充为方格,前景色为绿色,背景色为金色g.FillRectangle(brush2, 100, 100, 80, 80);,9.4.3TextureBrush纹理(图像)画刷,TextureBrush类的构造函数有以下两种: TextureBrush

24、 (Image image, Rectangle rect); TextureBrush (Image image,WrapMode wrapMode, Rectangle rect); 【例9-12】 在窗体上,使用图像填充图形。 private void Form1_Paint(object sender, PaintEventArgs e) Graphics g = this.CreateGraphics();Bitmap image1 = new Bitmap(“D:dark.jpg“);TextureBrush brush = new TextureBrush(image1, Wra

25、pMode.Tile);g.FillEllipse(brush, 20, 30, 250, 200); ,9.4.4LinearGradientBrush和 PathGradientBrush渐变画刷,线性渐变画刷LinearGradientBrush类常用的构造函数有以下几种: LinearGradientBrush ( Point point1, Point point2, Color color1, Color color2); LinearGradientBrush (Rectangle rect ,Color color1,Color color2,float angle); Pub

26、lic LinearGradientBrush(Rectangle rect ,Color color1,Color color2, LinearGradientMode linearGradientMode); 路径渐变画刷PathGradientBrush类常用的构造函数如下所示: PathGradientBrush( GraphicsPath path);,【例9-13】 在窗体上,使用渐变画刷填充图形。,private void Form1_Paint(object sender, PaintEventArgs e)Graphics g = this.CreateGraphics();

27、LinearGradientBrush brush1=new LinearGradientBrush( new Point(20,20),new Point (200,200),Color.White ,Color .Blue );g.FillEllipse (brush1 ,50,50,150,150);GraphicsPath path=new GraphicsPath (); path.AddRectangle(new Rectangle(200, 30, 200, 200);PathGradientBrush brush2=new PathGradientBrush (path );b

28、rush2.CenterPoint = new Point(280, 100);brush2.CenterColor = Color.Red;brush2.SurroundColors = new Color Color.Orange, Color.Gold, Color.GreenYello ;g.FillRectangle(brush2, new Rectangle(200, 50, 160, 150); ,9.5 绘制文本,Graphics对象使用DrawString方法绘制文本,方法如下:DrawString(string s, Font font,Brush brush, Point

29、 point);【例9-14】 在窗体上,绘制文本。 private void Form1_Paint(object sender, PaintEventArgs e)Graphics g= this.CreateGraphics();HatchBrush brush=new HatchBrush (HatchStyle.Cross ,Color .Red,Color .Blue );Font font1 = new Font(“隶书“,45, FontStyle.Bold | FontStyle.Italic);g.DrawString (“绘制文字!“,font1,brush ,new P

30、oint (50,50);Bitmap pic=new Bitmap (“D:dark.jpg“);TextureBrush brush2=new TextureBrush (pic);Font font2 = new Font(“姚体“,50, FontStyle.Bold | FontStyle.Italic);g.DrawString(“C#程序设计“,font2,brush2 ,new PointF (70,150);,9.6 Bitmap类,Bitmap类有多个重载的构造函数,常用形式如下所示: Bitmap(string filename); 例如: Bitmap bitmap =

31、 new Bitmap(“filename.jpg“); Public Bitmap(Image original); /通过指定的图像original创建Bitmap对象。,9.7 图像的处理,9.7.1显示图像 显示图像的方法,常用的有三种。 方法一:使用PictureBox图片控件,并设置PictureBox控件的image属性。 方法二:在程序中,通过“打开文件”对话框,选择图片文件设置PictureBox控件的image属性。 方法三:使用Bitmap类从文件中读取一个位图,并在屏幕中显示出图像。,9.7.2 保存图像,Save方法的构造函数有多种重载形式,常用形式为: Save(s

32、tring filename); Save(string filename, ImageFormat format ); 【例9-15】 将窗体上已经显示的图像保存为BMP格式。 private void Form1_Load(object sender, EventArgs e) OpenFileDialog dlg = new OpenFileDialog();dlg.Filter = “BMP文件(*.bmp)|*.bmp|JPEG文件(*.jpg)|*.jpg“;if (dlg.ShowDialog() = DialogResult.OK)Bitmap image = new Bitm

33、ap(dlg.FileName);pictureBox1.Image = image; ,private void button1_Click(object sender, EventArgs e) SaveFileDialog dlg = new SaveFileDialog();dlg.Filter = “BMP文件(*.bmp)|*.bmp“;Bitmap image = new Bitmap(pictureBox1.Image);dlg.ShowDialog();image.Save(dlg.FileName, System .Drawing .Imaging.ImageFormat.

34、Bmp ); ,9.7.3 彩色图片变为黑白图片,彩色图像处理成黑白效果通常有3种算法: (1) 最大值法: 使每个像素点的 R, G, B 值等于原像素点的 RGB (颜色值) 中最大的一个; (2) 平均值法: 使用每个像素点的 R,G,B值等于原像素点的RGB值的平均值; (3) 加权平均值法: 对每个像素点的 R, G, B值进行加权。,【例9-16】 将彩色图片变为黑白图片。,private void button1_Click(object sender, EventArgs e)Color c = new Color();Bitmap bitmap = new Bitmap(pi

35、ctureBox1.Image);Bitmap bitmap1 = new Bitmap(pictureBox1.Image);int r, g, b, avgColor;for (int i = 0; i pictureBox2.Width ; i+)for (int j = 0; j pictureBox2.Height ; j+),c = bitmap.GetPixel(i, j);r = c.R;g = c.G;b = c.B;avgColor = (int)(r + g + b) / 3);/求颜色的平均值if (avgColor 255) avgColor = 255;Color

36、c1 = Color.FromArgb(avgColor, avgColor, avgColor);/使用FromArgb方法把颜色的平均值赋给三种颜色,并转换成颜色值 bitmap1.SetPixel(i, j, c1);pictureBox2.Refresh();/刷新pictureBox2.Image = bitmap1;/bitmap1赋给图片框2,9.7.4 图片的翻转和旋转,DrawImage方法的形式如下所示: DrawImage(Image image,int x,int y,int w,int h); DrawImage(Image image,Rectangle rect)

37、; RotateFlip方法的形式如下: RotateFlip(RotateFlipType rotateFlipType); RotateFlipType参数是枚举值,部分枚举值含义如下所示: Rotate90FlipNone:不进行翻转的顺时针旋转90度 Rotate270FlipNone:不进行翻转的顺时针旋转270度 Rotate180FlipY:水平翻转 Rotate180FlipX:垂直翻转 Rotate180FlipXY:水平翻转和垂直翻转的 180 度旋转,【例9-17】 将给定图片旋转90度、旋转180度,水平翻转、垂直翻转。,private void radioButton

38、1_CheckedChanged(object sender, EventArgs e) Image img = pictureBox1.Image;img.RotateFlip(RotateFlipType.Rotate90FlipNone);pictureBox2.Image = img; private void radioButton2_CheckedChanged(object sender, EventArgs e) Image img = pictureBox1.Image;img.RotateFlip(RotateFlipType.Rotate180FlipNone);pict

39、ureBox2.Image = img; ,private void radioButton3_CheckedChanged(object sender, EventArgs e) Image img = pictureBox1.Image;img.RotateFlip(RotateFlipType.Rotate180FlipX);pictureBox2.Image = img;radioButton4.Enabled = true; private void radioButton4_CheckedChanged(object sender, EventArgs e) Image img = pictureBox1.Image;img.RotateFlip(RotateFlipType.Rotate180FlipY);pictureBox2.Image = img; private void Form1_Load(object sender, EventArgs e) pictureBox1 .Image =Image .FromFile (“D:2.jpg“); ,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 大学课件

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报