《C#面向对象程序设计及实践教程PPT第九章.pptx》由会员分享,可在线阅读,更多相关《C#面向对象程序设计及实践教程PPT第九章.pptx(38页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、第九章第九章 图形形图像像编程程主要内容:主要内容:9.1GDI+概述9.2Graphics类9.3基本图形的绘制和填充9.4常用画刷的创建及使用9.5绘制文本9.6Bitmap类9.7图像的处理9.8案例【教学目教学目标】掌握使用Graphics类绘制图形的基本步骤 了解常用的绘图对象 掌握C#中基本图形的绘制方法 了解画刷的创建及使用 了解绘制文本的方法 了解Bitmap类的常用属性和方法 掌握常用的图像处理方法9.1 GDI+概述概述GDI+(GraphiceDeviceInterfacePlus,图形设备接口)提供了各种丰富的图形图像处理功能在CGDI+由很多类和结构组成,使用GDI+
2、编写图形图像程序时,需要加入命名空间System.Drawing。9.2 Graphics类Graphics类是一个密封类,不能有派生类。9.2.1 使用使用Graphics类绘图的基本步的基本步骤使用Graphics类绘图的基本步骤:(1)创建Graphics对象(2)创建绘图工具(3)使用Graphics对象的方法绘图、显示文本或处理图像【例【例9-1】在窗体上用在窗体上用红色的画笔色的画笔绘制一个矩形。制一个矩形。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=e.Graphics;/定义Graphics对象gP
3、enmyPen=newPen(Color.Blue);/定义蓝色画笔对象myPeng.DrawRectangle(myPen,80,80,200,100);/绘制矩形窗体的窗体的Paint事件事件Windows应用程序的窗体和控件都有Paint事件。当窗体或控件上需要重新绘图时就会触发Paint事件。如果在控件的Paint事件中利用传递的参数获取Graphics对象,则绘制的图形图像仅在该控件内显示。如果在窗体的Paint事件中绘制,则绘制的图形图像在该窗体内显示。Form类窗体不能自动响应Paint事件,编程者必须将需要重新绘制图形的程序代码放置在窗体的Paint事件中,这样,才能实现窗体的
4、重绘。常用常用绘图对象象1.Point结构构Point结构表示在二维平面中定义点的整数x和y坐标的有序对。2.Size结构构Size结构用来存储一个有序整数对,通常为矩形的宽度和高度。Size结构中也有两个整数Width和Height,表示水平和垂直距离。3.Rectangle结构构存储一组整数,共四个,表示一个矩形的位置和大小。4.Color结构构使用Color结构可以定义颜色。任何一种颜色都由透明度(A)和红绿蓝三基色(R,G,B)组成。5.Pen类【例例9-3】在窗体上,定在窗体上,定义画笔,并画笔,并设置其属性置其属性。privatevoidForm1_Paint(objectsend
5、er,PaintEventArgse)Graphicsg=this.CreateGraphics();PenmyPen=newPen(Color.Red,5);/定义画笔对象myPenmyPen.DashStyle=System.Drawing.Drawing2D.DashStyle.DashDot;/设置画笔的虚线样式为划线点图案myPen.StartCap=System.Drawing.Drawing2D.LineCap.SquareAnchor;/设置线条的起点使用帽线为方锚头帽myPen.EndCap=System.Drawing.Drawing2D.LineCap.ArrowAnch
6、or;/设置线条的终点使用帽线为箭头状锚头帽g.DrawLine(myPen,50,50,200,200);9.3 基本基本图形的形的绘制和填充制和填充9.3.1 绘制制直直线voidDrawLine(Penpen,intx1,inty1,intx2,inty2);voidDrawLine(Penpen,Pointp1,Pointp2);voidDrawLines(Penpen,Pointpoint);【例9-4】在窗体上,绘制两条直线。Graphicsg=e.Graphics;PenmyPen=newPen(Color.Blue,2);Pointp1=newPoint(100,100);Po
7、intp2=newPoint(200,200);g.DrawLine(myPen,70,70,220,70);g.DrawLine(myPen,p1,p2);Pointpoints=newPoint(10,10),newPoint(40,40),newPoint(40,200),newPoint(220,230);g.DrawLines(myPen,points);9.3.2 绘制矩形制矩形绘制直制直线的常用方法:的常用方法:DrawRectangle(Penpen,RectanglerectDrawRectangle(Penpen,intx,inty,intwidth,intheight);
8、DrawRectangles(Penpen,Rectanglerects);可以使用Graphics对象提供的FillRectangle方法绘制一个填充矩形。FillRectangle(Brushbrush,intx,inty,intwidth,intheightFillRectangle(Brushbrush,Rectanglerect);FillRectangles(Brushbrush,Rectanglerect);【例【例9-5】在窗体上在窗体上绘制和填充矩形制和填充矩形。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graph
9、icsg=this.CreateGraphics();PenmyPen=newPen(Color.Green,3);Rectanglerect=newRectangle(50,50,200,150);BrushmyBrush=newSolidBrush(Color.Pink);g.DrawRectangle(myPen,rect);g.FillRectangle(myBrush,100,100,130,90);9.3.3 绘制多制多边形形Graphics对象使用DrawPolygon方法绘制多边形的轮廓,使用FillPolygon方法绘制填充多边形。DrawPolygon方法和FillPoly
10、gon方法常用形式如下:DrawPolygon(Penpen,Pointpoints)FillPolygon(Brushbrush,Pointpoints【例【例9-6】在窗体上在窗体上绘制和填充多制和填充多边形形。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=this.CreateGraphics();PenmyPen=newPen(Color.Red,2);Pointpoints1=newPoint(20,20),newPoint(100,60),newPoint(150,150),newPoint(10,150
11、);g.DrawPolygon(myPen,points1);BrushmyBrush=newSolidBrush(Color.Blue);Pointpoints2=newPoint(170,20),newPoint(230,20),newPoint(270,100),newPoint(230,200),newPoint(170,200);g.FillPolygon(myBrush,points2);9.3.4 绘制制圆和和椭圆Graphics对象使用DrawEllipse方法绘制圆和椭圆,使用FillEllipse方法绘制填充圆和椭圆。DrawEllipse方法用于绘制圆和椭圆,常用的形式如
12、下所示:DrawEllipse(Penpen,intx,inty,intwidth,intheight)DrawEllipse(Penpen,Rectanglerect)FillEllipse方法用于绘制填充圆和椭圆,常用的格式有:FillEllipse(Brushbrush,intx,inty,intwidth,intheight)FillEllipse(Brushbrush,Rectanglerect)【例【例9-7】在窗体上,在窗体上,绘制和填充制和填充圆和和椭圆。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=
13、this.CreateGraphics();PenmyPen=newPen(Color.Red,2);BrushmyBrush=newSolidBrush(Color.Blue);/定义画刷RectanglemyRect1=newRectangle(50,50,300,200);/定义矩形RectanglemyRect2=newRectangle(80,80,150,150);/定义正方形g.DrawEllipse(myPen,myRect1);/通过定义的外接矩形绘制椭圆g.FillEllipse(myBrush,myRect2);/通过定义的外接正方形绘制填充圆9.3.5 绘制弧制弧线Gr
14、aphics对象使用DrawArc方法绘制圆弧。DrawArc方法的常用结构如下所示:DrawArc(Penpen,intx,inty,intwidth,intheight,intstartAngle,intsweepAngle)【例9-8】在窗体上,绘制一段圆弧。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=this.CreateGraphics();PenmyPen=newPen(Color.Red,2);g.DrawArc(myPen,30,30,300,200,90,120);g.DrawArc(myPen,
15、30,30,300,200,-60,90);9.3.6 绘制扇形制扇形Graphics对象使用DrawPie方法绘制空心的扇形,使用FillPie方法绘制填充扇形。绘制空心扇形的DrawPie方法的常用形式有:DrawPie(Penpen,intx,inty,intwidth,intheight,intstartAngle,intsweepAngle)DrawPie(Penpen,Rectanglerect,floatstartAngle,floatsweepAngle)绘制填充扇形的FillPie方法的常用形式有:FillPie(Brushbrush,intx,inty,intwidth,i
16、ntheight,intstartAngle,intsweepAngle)FillPie(Brushbrush,Rectanglerect,intstartAngle,intsweepAngle)【例【例9-9】在窗体上,在窗体上,绘制和填充扇形制和填充扇形。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=this.CreateGraphics();PenmyPen=newPen(Color.Red,2);BrushmyBrush=newSolidBrush(Color.Gold);Rectanglerect=newR
17、ectangle(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(Penpen,Pointpoints);DrawCurve(Penpen,Pointpoints,floattension);DrawClosedCurve方法绘制闭合曲线:DrawClosedCurve(Penpen,
18、Pointpoints);FillClosedCurve(Brushbrush,Pointpoints);【例【例9-10】在窗体上,在窗体上,绘制和填充曲制和填充曲线。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=this.CreateGraphics();PenmyPen=newPen(Color.Red,2);BrushmyBrush=newSolidBrush(Color.Blue);Pointpoints1=newPoint(20,30),newPoint(50,60),newPoint(90,30),ne
19、wPoint(150,100);Pointpoints2=newPoint(50,200),newPoint(100,70),newPoint(120,200),newPoint(200,100),newPoint(250,50);g.DrawCurve(myPen,points1);g.FillClosedCurve(myBrush,points2);9.4 常用常用画刷的画刷的创建及使用建及使用SolidBrush画刷在命名空间System.Drawing中定义TextureBrush、LinearGradientBrush、PathGradientBrush、HatchBrush画刷类使
20、用时要加入命名空间类说明SolidBrush用纯色填充图形HatchBrush用各种图案填充图形TextureBrush用基于光栅的图像(位图、JPG等)填充图形LinearGradientBrush用颜色渐变填充图形PathGradientBrush用渐变效果填充图形9.4 常用画刷的常用画刷的创建及使用建及使用9.4.1 SolidBrush单色画刷色画刷例如:BrushmyBrush=newSolidBrush(Color.Red);SolidBrushbrush1=newSolidBrush(Coloe.Blue);9.4.2 HatchBrush 阴影画刷阴影画刷HatchBrush
21、类的构造函数有两种形式,如下所示:HatchBrush(HatchStylehatchStyle,ColorforeColor);HatchBrush(HatchStylehatchStyle,ColorforeColor,ColorbackColor);【例【例9-11】在窗体上,使用不同的阴影画刷填在窗体上,使用不同的阴影画刷填充充图形形。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=this.CreateGraphics();HatchBrushbrush1=newHatchBrush(HatchStyle.D
22、arkHorizontal,Color.Red);/椭圆填充为横线g.FillEllipse(brush1,20,20,100,60);HatchBrushbrush2=newHatchBrush(HatchStyle.Cross,Color.Green,Color.Gold);/正方形填充为方格,前景色为绿色,背景色为金色g.FillRectangle(brush2,100,100,80,80);纹理(理(图像)画像)画刷刷TextureBrush类的构造函数有以下两种:TextureBrush(Imageimage,Rectanglerect);TextureBrush(Imageimag
23、e,WrapModewrapMode,Rectanglerect);【例9-12】在窗体上,使用图像填充图形。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=this.CreateGraphics();Bitmapimage1=newBitmap(D:dark.jpg);TextureBrushbrush=newTextureBrush(image1,WrapMode.Tile);g.FillEllipse(brush,20,30,250,200);和和 PathGradientBrush渐变画画刷刷线性渐变画刷Lin
24、earGradientBrush类常用的构造函数有以下几种:LinearGradientBrush(Pointpoint1,Pointpoint2,Colorcolor1,Colorcolor2);LinearGradientBrush(Rectanglerect,Colorcolor1,Colorcolor2,floatangle);PublicLinearGradientBrush(Rectanglerect,Colorcolor1,Colorcolor2,LinearGradientModelinearGradientMode);路径渐变画刷PathGradientBrush类常用的构造
25、函数如下所示:PathGradientBrush(GraphicsPathpath);【例【例9-13】在窗体上,使用在窗体上,使用渐变画刷填充画刷填充图形形。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=this.CreateGraphics();LinearGradientBrushbrush1=newLinearGradientBrush(newPoint(20,20),newPoint(200,200),Color.White,Color.Blue);g.FillEllipse(brush1,50,50,15
26、0,150);GraphicsPathpath=newGraphicsPath();path.AddRectangle(newRectangle(200,30,200,200);PathGradientBrushbrush2=newPathGradientBrush(path);brush2.CenterPoint=newPoint(280,100);brush2.CenterColor=Color.Red;brush2.SurroundColors=newColorColor.Orange,Color.Gold,Color.GreenYello;g.FillRectangle(brush2,
27、newRectangle(200,50,160,150);9.5 绘制制文本文本Graphics对象使用DrawString方法绘制文本,方法如下:DrawString(strings,Fontfont,Brushbrush,Pointpoint);【例9-14】在窗体上,绘制文本。privatevoidForm1_Paint(objectsender,PaintEventArgse)Graphicsg=this.CreateGraphics();HatchBrushbrush=newHatchBrush(HatchStyle.Cross,Color.Red,Color.Blue);Fontf
28、ont1=newFont(隶书,45,FontStyle.Bold|FontStyle.Italic);g.DrawString(绘制文字!,font1,brush,newPoint(50,50);Bitmappic=newBitmap(D:dark.jpg);TextureBrushbrush2=newTextureBrush(pic);Fontfont2=newFont(姚体,50,FontStyle.Bold|FontStyle.Italic);g.DrawString(C#程序设计,font2,brush2,newPointF(70,150);9.6 Bitmap类Bitmap类有多个
29、重载的构造函数,常用形式如下所示:Bitmap(stringfilename);例如:Bitmapbitmap=newBitmap(filename.jpg);PublicBitmap(Imageoriginal);/通过指定的图像original创建Bitmap对象。9.7 图像像的的处理理显示示图像像显示图像的方法,常用的有三种。方法一:使用PictureBox图片控件,并设置PictureBox控件的image属性。方法二:在程序中,通过“打开文件”对话框,选择图片文件设置PictureBox控件的image属性。方法三:使用Bitmap类从文件中读取一个位图,并在屏幕中显示出图像。9.
30、7.2 保存保存图像像Save方法的构造函数有多种重载形式,常用形式为:Save(stringfilename);Save(stringfilename,ImageFormatformat);【例9-15】将窗体上已经显示的图像保存为BMP格式。privatevoidForm1_Load(objectsender,EventArgse)OpenFileDialogdlg=newOpenFileDialog();dlg.Filter=BMP文件(*.bmp)|*.bmp|JPEG文件(*.jpg)|*.jpg;if(dlg.ShowDialog()=DialogResult.OK)Bitmapi
31、mage=newBitmap(dlg.FileName);pictureBox1.Image=image;privatevoidbutton1_Click(objectsender,EventArgse)SaveFileDialogdlg=newSaveFileDialog();dlg.Filter=BMP文件(*.bmp)|*.bmp;Bitmapimage=newBitmap(pictureBox1.Image);dlg.ShowDialog();image.Save(dlg.FileName,System.Drawing.Imaging.ImageFormat.Bmp);9.7.3 彩色
32、彩色图片片变为黑白黑白图片片彩色图像处理成黑白效果通常有3种算法:(1)最大值法:使每个像素点的R,G,B值等于原像素点的RGB(颜色值)中最大的一个;(2)平均值法:使用每个像素点的R,G,B值等于原像素点的RGB值的平均值;(3)加权平均值法:对每个像素点的R,G,B值进行加权。【例【例9-16】将彩色将彩色图片片变为黑白黑白图片片。privatevoidbutton1_Click(objectsender,EventArgse)Colorc=newColor();Bitmapbitmap=newBitmap(pictureBox1.Image);Bitmapbitmap1=newBitm
33、ap(pictureBox1.Image);intr,g,b,avgColor;for(inti=0;ipictureBox2.Width;i+)for(intj=0;jpictureBox2.Height;j+)c=bitmap.GetPixel(i,j);r=c.R;g=c.G;b=c.B;avgColor=(int)(r+g+b)/3);/求颜色的平均值if(avgColor255)avgColor=255;Colorc1=Color.FromArgb(avgColor,avgColor,avgColor);/使用FromArgb方法把颜色的平均值赋给三种颜色,并转换成颜色值bitmap
34、1.SetPixel(i,j,c1);pictureBox2.Refresh();/刷新pictureBox2.Image=bitmap1;/bitmap1赋给图片框29.7.4 图片片的翻的翻转和和旋旋转DrawImage方法的形式如下所示:DrawImage(Imageimage,intx,inty,intw,inth);DrawImage(Imageimage,Rectanglerect);RotateFlip方法的形式如下:RotateFlip(RotateFlipTyperotateFlipType);RotateFlipType参数是枚举值,部分枚举值含义如下所示:Rotate90
35、FlipNone:不进行翻转的顺时针旋转90度Rotate270FlipNone:不进行翻转的顺时针旋转270度Rotate180FlipY:水平翻转Rotate180FlipX:垂直翻转Rotate180FlipXY:水平翻转和垂直翻转的180度旋转【例【例9-17】将将给定定图片旋片旋转90度、旋度、旋转180度,水平度,水平翻翻转、垂直翻、垂直翻转。privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)Imageimg=pictureBox1.Image;img.RotateFlip(RotateFlipType.R
36、otate90FlipNone);pictureBox2.Image=img;privatevoidradioButton2_CheckedChanged(objectsender,EventArgse)Imageimg=pictureBox1.Image;img.RotateFlip(RotateFlipType.Rotate180FlipNone);pictureBox2.Image=img;privatevoidradioButton3_CheckedChanged(objectsender,EventArgse)Imageimg=pictureBox1.Image;img.Rotate
37、Flip(RotateFlipType.Rotate180FlipX);pictureBox2.Image=img;radioButton4.Enabled=true;privatevoidradioButton4_CheckedChanged(objectsender,EventArgse)Imageimg=pictureBox1.Image;img.RotateFlip(RotateFlipType.Rotate180FlipY);pictureBox2.Image=img;privatevoidForm1_Load(objectsender,EventArgse)pictureBox1.Image=Image.FromFile(D:2.jpg);