图形图像与多媒体编程

上传人:ni****g 文档编号:567509427 上传时间:2024-07-20 格式:PPT 页数:88 大小:1.09MB
返回 下载 相关 举报
图形图像与多媒体编程_第1页
第1页 / 共88页
图形图像与多媒体编程_第2页
第2页 / 共88页
图形图像与多媒体编程_第3页
第3页 / 共88页
图形图像与多媒体编程_第4页
第4页 / 共88页
图形图像与多媒体编程_第5页
第5页 / 共88页
点击查看更多>>
资源描述

《图形图像与多媒体编程》由会员分享,可在线阅读,更多相关《图形图像与多媒体编程(88页珍藏版)》请在金锄头文库上搜索。

1、1图形图像与多媒体编程Stillwatersrundeep.流静水深流静水深,人静心深人静心深Wherethereislife,thereishope。有生命必有希望。有生命必有希望214.1GDI+概述概述 GDI+:GraphicsDeviceInterfacePlus,它提供了各种,它提供了各种丰富的图形图像处理功能丰富的图形图像处理功能在在C#.NETC#.NET中,使用中,使用GDI+GDI+处理二维(处理二维(2D2D)的图形和图像,)的图形和图像,使用使用DirectXDirectX处理三维(处理三维(3D3D)的图形图像。)的图形图像。 GDI+主要有主要有“二维矢量图形二维矢

2、量图形”、“图像处理图像处理”和和“版式版式”三部分组成。三部分组成。GDI+提供了存储基元自身相关信息的类和结构、存储基提供了存储基元自身相关信息的类和结构、存储基元绘制方式相关信息的类,以及实际进行绘制的类。元绘制方式相关信息的类,以及实际进行绘制的类。GDI+为使用各种字体、字号和样式来显示文本这种复为使用各种字体、字号和样式来显示文本这种复杂任务提供了大量的支持。杂任务提供了大量的支持。其他高级功能其他高级功能3在在C#C#中,所有图形图像处理功能都包含在以下名称空间下:中,所有图形图像处理功能都包含在以下名称空间下:1. System.Drawing1. System.Drawing

3、名称空间名称空间 提供了提供了对GDI+GDI+基本基本图形功能的形功能的访问,主要有,主要有GraphicsGraphics类、BitmapBitmap类、从、从BrushBrush类继承的承的类、FontFont类、IconIcon类、ImageImage类、PenPen类、ColorColor类等等。2. System.Drawing.Drawing2D2. System.Drawing.Drawing2D名称空间名称空间 提供了高级的二维和矢量图形功能。主要有梯度型画提供了高级的二维和矢量图形功能。主要有梯度型画刷、刷、MatrixMatrix类(用于定义几何变换)和类(用于定义几何变

4、换)和GraphicsPathGraphicsPath类等。类等。 3.System.Drawing.Imaging名称空间名称空间 提供了高级提供了高级GDI+图像处理功能。图像处理功能。4. System.Drawing.Text4. System.Drawing.Text名称空间名称空间 提供了高级提供了高级GDI+字体和文本排版功能字体和文本排版功能414.1.1Graphics类类 GraphicsGraphics类类包包含含在在System.DrawingSystem.Drawing名名称称空空间间下下。要要进进行行图图形形处处理理,必必须须首首先先创创建建GraphicsGrap

5、hics对对象象,然然后后才才能能利利用用它它进进行各种画图操作。行各种画图操作。 创建创建GraphicsGraphics对象的形式有:对象的形式有: 1. 1.在窗体或控件的在窗体或控件的PaintPaint事件中直接引用事件中直接引用GraphicsGraphics对象对象 每每一一个个窗窗体体或或控控件件都都有有一一个个PaintPaint事事件件,该该事事件件的的参参数数中中包包含含了了当当前前窗窗体体或或控控件件的的GraphicsGraphics对对象象,在在为为窗窗体体或或控控件件创建绘制代码时,一般使用此方法来获取对图形对象的引用。创建绘制代码时,一般使用此方法来获取对图形对

6、象的引用。Private void Form_Paint(object sender,Private void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e) Graphics g=e.Graphics; Graphics g=e.Graphics; 5 2. 2.从当前窗体获取对从当前窗体获取对GraphicsGraphics对象的引用对象的引用 把把当当前前窗窗体体的的画画刷刷、字字体体、颜颜色色作作为为缺缺省省值值获获取取对对Gr

7、aphicsGraphics对对象象的的引引用用,注注意意这这种种对对象象只只有有在在处处理理当当前前WindowsWindows窗窗口口消消息息的的过过程程中中有有效效。如如果果想想在在已已存存在在的的窗窗体体或控件上绘图,可以使用此方法。例如:或控件上绘图,可以使用此方法。例如: Graphics g=this.CreatGraphics(); Graphics g=this.CreatGraphics(); 3. 3.从继承自图像的任何对象创建从继承自图像的任何对象创建GraphicsGraphics对象。对象。 此方法在需要更改已存在的图像时十分有用。例如:此方法在需要更改已存在的图像

8、时十分有用。例如: Bitmap bitmap=new Bitmap( Bitmap bitmap=new Bitmap(C:testa1.bmpC:testa1.bmp);); Graphics g=Graphics.FromImage(bitmap); Graphics g=Graphics.FromImage(bitmap);614.1.2颜色颜色 颜颜色色是是进进行行图图形形操操作作的的基基本本要要素素。任任何何一一种种颜颜色色都都可可以以由四个分量决定,每个分量占据一个字节:由四个分量决定,每个分量占据一个字节: R R:红色,取值范围:红色,取值范围0 0255255,255255

9、为饱和红色。为饱和红色。 G G:绿色,取值范围:绿色,取值范围0 0255255,255255为饱和绿色。为饱和绿色。 B B:蓝色,取值范围:蓝色,取值范围0 0255255,255255为饱和蓝色。为饱和蓝色。 A A:AlphaAlpha值值,即即透透明明度度。取取值值范范围围0 0255255,0 0为为完完全全透透明明,255255为完全不透明。为完全不透明。 在在System.System.DrawingDrawing名名称称空空间间下下,有有一一个个ColorColor结结构构类类型型,可可以使用下列方法创建颜色对象:以使用下列方法创建颜色对象: 使用使用FromArgbFro

10、mArgb指定任意颜色指定任意颜色 这这个个方方法法有有两两种种常常用用的的形形式式,第第一一种种形形式式是是直直接接指指定定三三种颜色,方法原型为:种颜色,方法原型为:7 public static Color FromArgb(int red,int green,int blue)public static Color FromArgb(int red,int green,int blue)三三个个参参数数分分别别表表示示R R、G G、B B三三色色,AlphaAlpha值值使使用用缺缺省省值值255255,即即完完全全不不透透明。例如:明。例如:Color red=Color.From

11、Argb(255,0,0);Color red=Color.FromArgb(255,0,0);Color green=Color.FromArgb(0,255,0);Color green=Color.FromArgb(0,255,0);Color blue=Color.FromArgb(0,0,0xff);Color blue=Color.FromArgb(0,0,0xff); 其中,其中,0xff0xff为十六进制表示形式。为十六进制表示形式。第二种形式使用四个参数,格式为:第二种形式使用四个参数,格式为:public public static static Color Color F

12、romArgb(int FromArgb(int alpha,int alpha,int red,int red,int green,int green,int blue)blue)四个参数分别表示透明度和四个参数分别表示透明度和R R、G G、B B三色值。三色值。8 使用系统预定义颜色使用系统预定义颜色 在在ColorColor结结构构中中已已经经预预定定义义了了141141种种颜颜色色,可可以以直直接接使使用,例如:用,例如: Color myColor; Color myColor; myColor = Color.Red; myColor = Color.Red; myColor =

13、 Color.Aquamarine; myColor = Color.Aquamarine; myColor = Color.LightGoldenrodYellow; myColor = Color.LightGoldenrodYellow;914.1.3笔和画笔笔和画笔 在在GDI+GDI+中中,可可使使用用笔笔对对象象和和画画笔笔对对象象呈呈现现图图形形、文文本本和和图图像像。笔笔是是PenPen类类的的实实例例,用用于于绘绘制制线线条条和和空空心心形形状状。画画笔笔是是从从BrushBrush类类派派生生的的任任何何类类的的实实例例,用用于于填填充充形形状状或或绘绘制制文文本。本。1.

14、笔(笔(Pen) 笔笔可可用用于于绘绘制制绘绘制制具具有有指指定定宽宽度度和和样样式式的的线线条条、曲曲线线以以及勾勒形状轮廓。及勾勒形状轮廓。 下面的示例说明如何创建一支基本的黑色笔:下面的示例说明如何创建一支基本的黑色笔: Pen myPen = new Pen(Color.Black); Pen myPen = new Pen(Color.Black); Pen myPen = new Pen(Color.Black, 5); Pen myPen = new Pen(Color.Black, 5); 也可以从画笔对象创建笔,例如:也可以从画笔对象创建笔,例如: SolidBrush my

15、Brush = new SolidBrush(Color.Red); SolidBrush myBrush = new SolidBrush(Color.Red); Pen myPen = new Pen(myBrush); Pen myPen = new Pen(myBrush); Pen myPen = new Pen(myBrush, 5); Pen myPen = new Pen(myBrush, 5); 10笔(笔(PenPen)的用法演示示例。)的用法演示示例。 1) 1) 新新建建一一个个WindowsWindows应应用用程程序序,适适当当加加宽宽窗窗体体宽宽度度。然后切换到代

16、码方式,添加名称空间引用:然后切换到代码方式,添加名称空间引用: using System.Drawing.Drawing2D; using System.Drawing.Drawing2D; 2) 2) 添加添加Form1_PaintForm1_Paint事件代码。事件代码。 private void Form1_Paint(object sender, private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e) Gr

17、aphics g = e.Graphics;Graphics g = e.Graphics;Pen pen = new Pen(Color.Blue,10.5f); Pen pen = new Pen(Color.Blue,10.5f); g.DrawStringg.DrawString(蓝色,宽度为蓝色,宽度为10.5,this.Font,10.5,this.Font, new SolidBrush(Color.Black),5,5); new SolidBrush(Color.Black),5,5);g.DrawLine(pen,new Point(110,10),new Point(38

18、0,10);g.DrawLine(pen,new Point(110,10),new Point(380,10);pen.Width=2;pen.Width=2; pen.Color=Color.Red; pen.Color=Color.Red;g.DrawString(g.DrawString(红色,宽度为红色,宽度为2,this.Font,2,this.Font, new SolidBrush(Color.Black),5,25); new SolidBrush(Color.Black),5,25);11g.DrawLine(pen,new Point(110,30),new Point(

19、380,30);g.DrawLine(pen,new Point(110,30),new Point(380,30);pen.StartCap=LineCap.Flat;pen.StartCap=LineCap.Flat;pen.EndCap=LineCap.ArrowAnchor;pen.EndCap=LineCap.ArrowAnchor;pen.Width=9;pen.Width=9;g.DrawString(g.DrawString(红色箭头线红色箭头线,this.Font,this.Font, new new SolidBrush(Color.Black),5,45);SolidBr

20、ush(Color.Black),5,45);g.DrawLine(pen,new Point(110,50),new Point(380,50);g.DrawLine(pen,new Point(110,50),new Point(380,50);pen.DashStyle = DashStyle.Custom;pen.DashStyle = DashStyle.Custom;pen.DashPattern=new float4,4;pen.DashPattern=new float4,4;pen.Width=2;pen.Width=2; pen.EndCap=LineCap.NoAncho

21、r; pen.EndCap=LineCap.NoAnchor;g.DrawString(g.DrawString(自定义虚线自定义虚线,this.Font,this.Font, new SolidBrush(Color.Black),5,65); new SolidBrush(Color.Black),5,65);g.DrawLine(pen,new Point(110,40),new Point(380,70);g.DrawLine(pen,new Point(110,40),new Point(380,70);pen.DashStyle=DashStyle.Dot;pen.DashStyl

22、e=DashStyle.Dot;g.DrawString(g.DrawString(点划线点划线,this.Font,this.Font, new SolidBrush(Color.Black),5,85); new SolidBrush(Color.Black),5,85);g.DrawLine(pen,new Point(110,90),new Point(380,90);g.DrawLine(pen,new Point(110,90),new Point(380,90); 12运行结果运行结果132 2、画刷(、画刷(BrushBrush) 画画刷刷是是可可与与GraphicsGraph

23、ics对对象象一一起起使使用用来来创创建建实实心心形形状状和和呈呈现现文文本本的的对对象象。可可以以用用画画笔笔填填充充各各种种图图形形形形状状,如如矩形、椭圆、扇形、多边形和封闭路径等。矩形、椭圆、扇形、多边形和封闭路径等。 几种不同类型的画刷几种不同类型的画刷: : SolidBrush SolidBrush 画刷最简单的形式,用纯色进行绘制。画刷最简单的形式,用纯色进行绘制。 HatchBrush HatchBrush 类似于类似于 SolidBrush SolidBrush,但是可以利用该类从大量预设,但是可以利用该类从大量预设的图案中选择绘制时要使用的图案,而不是纯色。的图案中选择绘

24、制时要使用的图案,而不是纯色。 TextureBrush TextureBrush 使用纹理(如图像)进行绘制。使用纹理(如图像)进行绘制。 LinearGradientBrush LinearGradientBrush 使用沿渐变混合的两种颜色进行绘制。使用沿渐变混合的两种颜色进行绘制。 PathGradientBrush PathGradientBrush 基于编程者定义的唯一路径,使用复杂的混合色渐变基于编程者定义的唯一路径,使用复杂的混合色渐变进行绘制。进行绘制。14(1)(1)使用使用SolidBrushSolidBrush类定义单色画笔类定义单色画笔 SolidBrushSolid

25、Brush类类用用于于定定义义单单色色画画笔笔。该该类类只只有有一一个个构构造函数,带有一个造函数,带有一个ColorColor类型的参数。类型的参数。 下下面面的的示示例例说说明明如如何何在在窗窗体体上上绘绘制制一一个个纯纯红红色色的的椭椭圆圆。该该椭椭圆圆将将符符合合为为其其提提供供的的矩矩形形的的大大小小(此此例例中中为为表表示整个窗体的示整个窗体的ClientRectangleClientRectangle)。)。【例】单色画刷演示示例。【例】单色画刷演示示例。 private void Form1_Paint(object sender, private void Form1_Pai

26、nt(object sender, System.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e) Graphics g =e.Graphics;Graphics g =e.Graphics;SolidBrush myBrush = new SolidBrush(Color.Red);SolidBrush myBrush = new SolidBrush(Color.Red);g.FillEllipse(myBrush, this.ClientRectangle);g.FillEllipse(myBru

27、sh, this.ClientRectangle); 15运行效果运行效果16(2)(2)使用使用HatchBrushHatchBrush类绘制简单图案类绘制简单图案 HatchBrushHatchBrush类类用用于于从从大大量量预预设设的的图图案案中中选选择择绘绘制制时时要使用的图案,而不是纯色。要使用的图案,而不是纯色。 下下面面的的示示例例说说明明如如何何创创建建一一个个HatchBrushHatchBrush,它它使使用用90%90%的的阴阴影影,前前景景色色与与背背景景色色的的比比例例为为90:10090:100,并并使使用用白白色作为前景色,黑色作为背景色。色作为前景色,黑色作为

28、背景色。【例】填充简单图案示例。【例】填充简单图案示例。 privateprivate voidvoid Form1_Paint( Form1_Paint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e)System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics;Graphics g = e.Graphics;System.Drawing.Drawing2D.HatchBrush aHatchBrush = System.Drawing.Dra

29、wing2D.HatchBrush aHatchBrush = newnew System.Drawing.Drawing2D.HatchBrush(System.Drawing.System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent90, Color.White, Color.Black);Drawing2D.HatchStyle.Percent90, Color.White, Color.Black);g.FillEllipse(aHatchBrush, g.FillEllipse(aH

30、atchBrush, thisthis.ClientRectangle);.ClientRectangle); 17运行效果:运行效果:18(3)(3)使用使用TextureBrushTextureBrush类绘制复杂图案类绘制复杂图案 TextureBrushTextureBrush类类允允许许使使用用一一幅幅图图像像作作为为填填充充的的样样式式。该该类提供了类提供了5 5个重载的构造函数,分别是:个重载的构造函数,分别是: Public TextureBrush(Image) Public TextureBrush(Image) Public TextureBrush(Image,Rect

31、angle) Public TextureBrush(Image,Rectangle) Public TextureBrush(Image,WrapMode) Public TextureBrush(Image,WrapMode) Public TextureBrush(Image,Rectangle,ImageAttributes) Public TextureBrush(Image,Rectangle,ImageAttributes) Public TextureBrush(Image,WrapMode,Rectangle) Public TextureBrush(Image,WrapMo

32、de,Rectangle) 其中:其中:ImageImage:ImageImage对象用于指定画笔的填充图案。对象用于指定画笔的填充图案。 Rectangle Rectangle:RectangleRectangle对象用于指定图像上用于画笔的矩形区对象用于指定图像上用于画笔的矩形区 域,其位置不能超越图像的范围。域,其位置不能超越图像的范围。 WrapMode WrapMode:WrapModeWrapMode枚举成员用于指定如何排布图像,可以是枚举成员用于指定如何排布图像,可以是 Clamp Clamp 完全由绘制对象的边框决定完全由绘制对象的边框决定 Tile Tile 平铺平铺 Til

33、eFlipX TileFlipX 水平方向翻转并平铺图像水平方向翻转并平铺图像 TileFlipY TileFlipY 垂直方向翻转并平铺图像垂直方向翻转并平铺图像 TileFlipXY TileFlipXY 水平和垂直方向翻转并平铺图像水平和垂直方向翻转并平铺图像19 ImageAttributes ImageAttributes:ImageAttributesImageAttributes对象用于指定图像的附加特对象用于指定图像的附加特 性参数。性参数。 TextureBrush TextureBrush类有三个属性:类有三个属性: Image Image:ImageImage类型,与画笔

34、关联的图像对象。类型,与画笔关联的图像对象。 Transform Transform:MatrixMatrix类型,画笔的变换矩阵。类型,画笔的变换矩阵。 WrapMode WrapMode:WrapModeWrapMode枚举成员,指定图像的排布方式。枚举成员,指定图像的排布方式。 下面的示例说明了如何创建一个下面的示例说明了如何创建一个TextureBrush,例子使用,例子使用名为名为m23.jpg的图像进行绘制。的图像进行绘制。【例】创建【例】创建TextureBrushTextureBrush示例。示例。 privateprivate voidvoid Form1_Paint( Fo

35、rm1_Paint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e)System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics;Graphics g = e.Graphics;TextureBrush myBrush = TextureBrush myBrush = newnew TextureBrush( TextureBrush(newnew Bitmap(e:testm23.jpg);Bitmap(e:testm23.jpg);g.Fil

36、lEllipse(myBrush, g.FillEllipse(myBrush, thisthis.ClientRectangle);.ClientRectangle); 20运行效果:运行效果:21(4)(4)使用使用LinearGradientBrushLinearGradientBrush类定义线性渐变类定义线性渐变 这这个个类类用用于于定定义义线线性性渐渐变变画画笔笔,可可以以是是双双色色渐渐变变,也也可可以以是是多多色色渐渐变变。缺缺省省情情况况下下,渐渐变变由由起起始始颜颜色色沿沿着着水水平平方方向向平平均均过过渡渡到到终终止止颜颜色色。要要定定义义多多色色渐渐变变,需需要要使使用

37、用InterpolationColorsInterpolationColors属属性性。下下面面的的示示例例说说明明如如何何由由白色渐变到蓝色。白色渐变到蓝色。【例】线性渐变示例。【例】线性渐变示例。 privateprivate voidvoid Form1_Paint( Form1_Paint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e)System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics;Graphics g = e.Graph

38、ics;System.Drawing.Drawing2D.LinearGradientBrush myBrush = System.Drawing.Drawing2D.LinearGradientBrush myBrush = new new System.Drawing.Drawing2D.LinearGradientBrush( System.Drawing.Drawing2D.LinearGradientBrush( thisthis.ClientRectangle,Color.White, Color.Blue,.ClientRectangle,Color.White, Color.B

39、lue, System.Drawing.Drawing2D.LinearGradientMode.Vertical); System.Drawing.Drawing2D.LinearGradientMode.Vertical); g.FillRectangle(myBrush, g.FillRectangle(myBrush, thisthis.ClientRectangle);.ClientRectangle); 22 如果创建应用程序后向设计窗体上拖放一些控件,如果创建应用程序后向设计窗体上拖放一些控件,可以看到运行后该图就是一个漂亮的背景了。可以看到运行后该图就是一个漂亮的背景了。23(

40、5)(5)使用使用PathGradientBrushPathGradientBrush类实现彩色渐变类实现彩色渐变 在在GDI+GDI+中中,把把一一个个或或多多个个图图形形组组成成的的形形体体称称作作路路径径。可可以以使使用用GraphicsPathGraphicsPath类类定定义义路路径径,使使用用PathGradientBrushPathGradientBrush类类定定义义路路径径内内部部的的渐渐变变色色画画笔笔。渐渐变变色色从从路路径径内内部部的的中中心心点点逐渐过渡到路径的外边界边缘。逐渐过渡到路径的外边界边缘。 PathGradientBrush PathGradientBru

41、sh类有三种形式的构造函数,形式之一是:类有三种形式的构造函数,形式之一是: public PathGradientBrush(GraphicsPath path) public PathGradientBrush(GraphicsPath path) 其中,其中,GraphicsPathGraphicsPath定义画笔填充的区域。定义画笔填充的区域。【例】路径和路径画笔的使用。【例】路径和路径画笔的使用。 using System.Drawing.Drawing2D;using System.Drawing.Drawing2D; 24private void Form1_Paint(obje

42、ct sender,private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e) Graphics g=e.Graphics;Graphics g=e.Graphics;Point centerPoint=new Point(150,100);Point centerPoint=new Point(150,100);int R=60;int R=60; GraphicsPath path=new GraphicsPath(

43、); GraphicsPath path=new GraphicsPath();path.AddEllipse(centerPoint.X-R,centerPoint.Y-R,2*R,2*R);path.AddEllipse(centerPoint.X-R,centerPoint.Y-R,2*R,2*R);PathGradientBrush brush=new PathGradientBrush(path);PathGradientBrush brush=new PathGradientBrush(path); / /指定路径中心点指定路径中心点brush.CenterPoint=center

44、Point;brush.CenterPoint=centerPoint;/指定路径中心点的颜色指定路径中心点的颜色brush.CenterColor=Color.Red;brush.CenterColor=Color.Red;/Color/Color类型的数组指定与路径上每个顶点对应的颜色类型的数组指定与路径上每个顶点对应的颜色brush.SurroundColors=new Color Color.Plum ;brush.SurroundColors=new Color Color.Plum ;25 g.FillEllipse(brush,centerPoint.X-R,centerPoin

45、t.Y-R,g.FillEllipse(brush,centerPoint.X-R,centerPoint.Y-R, 2*R,2*R); 2*R,2*R); centerPoint=new Point(350,100); centerPoint=new Point(350,100); R=20; R=20;path=new GraphicsPath();path=new GraphicsPath();path.AddEllipse(centerPoint.X-R,centerPoint.Y-R,2*R,2*R);path.AddEllipse(centerPoint.X-R,centerPoi

46、nt.Y-R,2*R,2*R);path.AddEllipse(centerPoint.X-2*R,centerPoint.Y-2*R,path.AddEllipse(centerPoint.X-2*R,centerPoint.Y-2*R, 4*R,4*R); 4*R,4*R);path.AddEllipse(centerPoint.X-3*R,centerPoint.Y-3*R,path.AddEllipse(centerPoint.X-3*R,centerPoint.Y-3*R, 6*R,6*R); 6*R,6*R); brush=new PathGradientBrush(path);

47、brush=new PathGradientBrush(path);brush.CenterPoint=centerPoint;brush.CenterPoint=centerPoint;brush.CenterColor=Color.Red;brush.CenterColor=Color.Red;brush.SurroundColors=new brush.SurroundColors=new Color Color.Black,Color.Blue,Color.Green ; Color Color.Black,Color.Blue,Color.Green ;g.FillPath(brus

48、h,path);g.FillPath(brush,path); 26 在这个例子中,可以看到当使用在这个例子中,可以看到当使用FillPath()FillPath()方法填充路径的时方法填充路径的时候,如果多个图形互相重叠,则重叠部分的数目为偶数时不会被填候,如果多个图形互相重叠,则重叠部分的数目为偶数时不会被填充,因此右图中间部分仍为背景色而不是蓝色。充,因此右图中间部分仍为背景色而不是蓝色。2714.1.4平移、旋转与缩放平移、旋转与缩放 GraphicsGraphics类类提提供供了了三三种种对对图图像像进进行行几几何何变变换换的的方方法法,它它们们是是TranslateTransfor

49、m()TranslateTransform()方方法法、RotateTransform()RotateTransform()方方法法和和ScaleTransform()ScaleTransform()方方法法,分分别别用用于于图图形形图图像像的的平平移移、旋旋转和缩放。转和缩放。 TranslateTransform()TranslateTransform()方法的形式为:方法的形式为: public void TranslateTransform(float dx,float dy) public void TranslateTransform(float dx,float dy) 其中,其

50、中,dxdx表示平移的表示平移的x x分量,分量,dydy表示平移的表示平移的y y分量。分量。 RotateTransform()RotateTransform()方法的形式为:方法的形式为: public void RotateTransform(float angle) public void RotateTransform(float angle) 其中,其中,angleangle表示旋转角度。表示旋转角度。 ScaleTransform()ScaleTransform()方法的形式为:方法的形式为: public void ScaleTransform(float sx,float

51、sy) public void ScaleTransform(float sx,float sy) 其中,其中,sx表示表示x方向的缩放比例,方向的缩放比例,sy表示表示y方向的缩放比例。方向的缩放比例。28【例】三种变换方法示例。【例】三种变换方法示例。 privateprivate voidvoid Form1_Paint( Form1_Paint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e) Graphics g = e.

52、Graphics;Graphics g = e.Graphics;/椭圆透明度椭圆透明度80%80%g.FillEllipse(g.FillEllipse(newnew SolidBrush(Color.FromArgb(80,Color.Red), SolidBrush(Color.FromArgb(80,Color.Red), 120,30,200,100); 120,30,200,100);g.RotateTransform(30.0f); g.RotateTransform(30.0f); /顺时针旋转顺时针旋转1010度度g.FillEllipse(g.FillEllipse(new

53、 new SolidBrush(Color.FromArgb(80,Color.Blue),SolidBrush(Color.FromArgb(80,Color.Blue), 120,30,200,100); 120,30,200,100);/水平方向向右平移水平方向向右平移200200个像素,垂直方向向上平移个像素,垂直方向向上平移100100个像素个像素g.TranslateTransform(200.0f,-100.0f);g.TranslateTransform(200.0f,-100.0f);g.FillEllipse(g.FillEllipse(new new SolidBrush

54、(Color.FromArgb(50,Color.Green),SolidBrush(Color.FromArgb(50,Color.Green), 120,30,200,100); 120,30,200,100);g.ScaleTransform(0.5f,0.5f); g.ScaleTransform(0.5f,0.5f); /缩小到一半缩小到一半 g.FillEllipse( g.FillEllipse(newnewSolidBrush(Color.FromArgb(100,Color.Red),SolidBrush(Color.FromArgb(100,Color.Red), 120,

55、30,200,100); 120,30,200,100); 293014.2绘制图形绘制图形 所有绘制图形的方法都位于所有绘制图形的方法都位于GraphicsGraphics中。中。14.2.1直线直线 有两种绘制直线的方法:有两种绘制直线的方法:DrawLine()DrawLine()方法和方法和DrawLines()DrawLines()方法。方法。 DrawLine()DrawLine()用用于于绘绘制制一一条条直直线线,DrawLines()DrawLines()用用于于绘绘制制多多条条直直线线。常常用形式有:用形式有: public void DrawLine(Pen pen,Poi

56、nt pt1,Point pt2) public void DrawLine(Pen pen,Point pt1,Point pt2) 其其中中PenPen对对象象确确定定线线条条的的颜颜色色、宽宽度度和和样样式式。PointPoint结结构构确确定定起起点点和和终终点。例如:点。例如:privateprivate voidvoid Form1_Paint( Form1_Paint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e)

57、Graphics g = e.Graphics;Graphics g = e.Graphics;Pen blackPen = Pen blackPen = newnew Pen(Color.Black, 3); Pen(Color.Black, 3);Point point1 = Point point1 = newnew Point(100, 100); Point(100, 100);Point point2 = Point point2 = newnew Point(200, 100); Point(200, 100);e.Graphics.DrawLine(blackPen, poin

58、t1, point2);e.Graphics.DrawLine(blackPen, point1, point2); 31 public void DrawLine(Pen pen,int x1,int y1,int x2,int y2)public void DrawLine(Pen pen,int x1,int y1,int x2,int y2) 其中其中x1,y1x1,y1为起点坐标,为起点坐标,x2,y2x2,y2为终点坐标。例如:为终点坐标。例如:e.Graphics.DrawLine(blackPen, 100,100,200,100);e.Graphics.DrawLine(bl

59、ackPen, 100,100,200,100); public void DrawLines(Pen pen,Point points) public void DrawLines(Pen pen,Point points) 这种方法用于绘制一系列连接一组终结点的线条。数组中的前两个这种方法用于绘制一系列连接一组终结点的线条。数组中的前两个点指定第一条线。每个附加点指定一个线段的终结点,该线段的起点指定第一条线。每个附加点指定一个线段的终结点,该线段的起始点是前一条线段的结束点。始点是前一条线段的结束点。privateprivate voidvoid Form1_Paint( Form1_P

60、aint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics;Graphics g = e.Graphics;Pen pen = Pen pen = newnew Pen(Color.Black, 3); Pen(Color.Black, 3);Point points =Point points =newnew Point( 10, 10), Point( 10, 10),newnew

61、 Point( 10, 100), Point( 10, 100),newnew Point(200, 50), Point(200, 50),newnew Point(250, 120) Point(250, 120); e.Graphics.DrawLines(pen, points); e.Graphics.DrawLines(pen, points); 32效果效果3314.2.2矩形矩形 由由于于矩矩形形具具有有轮轮廓廓和和封封闭闭区区域域,所所以以C#C#提提供供了了两两类类绘绘制制矩矩形形的的方方法法,一一类类用用于于绘绘制制矩矩形形的的轮轮廓廓,另另一一类类用用于于填充矩形的封

62、闭区域。填充矩形的封闭区域。 使使用用DrawRectangle()DrawRectangle()或或DrawRectangles()DrawRectangles()方方法法绘绘制制矩矩形轮廓的常用形式有:形轮廓的常用形式有:public public void void DrawRectangle(Pen DrawRectangle(Pen pen, pen, Rectangle Rectangle rect)rect) 其中其中rectrect表示要绘制的矩形的表示要绘制的矩形的RectangleRectangle结构。结构。【例】绘制矩形轮廓示例。【例】绘制矩形轮廓示例。 private

63、private voidvoid Form1_Paint( Form1_Paint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e)System.Windows.Forms.PaintEventArgs e)Graphics g = e.Graphics;Graphics g = e.Graphics;Pen pen = Pen pen = newnew Pen(Color.Black, 3); Pen(Color.Black, 3);Rectangle rect = Rectangle rect = new

64、new Rectangle( 30, 30, 200, 100); Rectangle( 30, 30, 200, 100);e.Graphics.DrawRectangle(pen, rect);e.Graphics.DrawRectangle(pen, rect);34 public public void void DrawRectangle(Pen DrawRectangle(Pen pen, pen, int int x, x, int int y, y, int int width, int height)width, int height) 其中其中x, yx, y为矩形左上角坐

65、标值。例如:为矩形左上角坐标值。例如: e.Graphics.DrawRectangle(pen, 20,20,200,100); e.Graphics.DrawRectangle(pen, 20,20,200,100); public void DrawRectangles(Pen pen, Rectangle rects)public void DrawRectangles(Pen pen, Rectangle rects) 该方法用于绘制多个矩形。该方法用于绘制多个矩形。【例】使用【例】使用DrawRectanglesDrawRectangles方法绘制矩形轮廓示例。方法绘制矩形轮廓示例

66、。 privateprivate voidvoid Form1_Paint( Form1_Paint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e)System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics;Graphics g = e.Graphics;Pen pen = Pen pen = newnew Pen(Color.Black, 3); Pen(Color.Black, 3);Rectangle rects =Rectangle

67、rects =newnew Rectangle( 0, 0, 100, 200), Rectangle( 0, 0, 100, 200),newnew Rectangle(100, 200, 250, 50), Rectangle(100, 200, 250, 50),newnew Rectangle(300, 0, 50, 100) Rectangle(300, 0, 50, 100);e.Graphics.DrawRectangles(pen, rects);e.Graphics.DrawRectangles(pen, rects); 353614.2.3多边形多边形 由由于于多多边边形形

68、也也是是封封闭闭的的,所所以以C#C#中中也也有有两两种种绘绘制制方方法法 : 使使 用用 DrawPolygon()DrawPolygon()方方 法法 绘绘 制制 多多 边边 形形 轮轮 廓廓 , 使使 用用 FillPolygonFillPolygon()()方法填充多边形的封闭区域。方法填充多边形的封闭区域。 下面的例子说明了这些方法的使用形式。下面的例子说明了这些方法的使用形式。【例】绘制多边形示例。【例】绘制多边形示例。 privateprivate voidvoid Form1_Paint( Form1_Paint(objectobject sender, sender, Sys

69、tem.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics;Graphics g = e.Graphics;Pen pen = Pen pen = newnew Pen(Color.Red); Pen(Color.Red);Point points =Point points = newnew Point(50,50), Point(50,50),newnew Point(100,100), Point(100,100), newnew Point(45,1

70、50), Point(45,150), newnew Point(25,150), Point(25,150),newnew Point(0,100) Point(0,100);e.Graphics.DrawPolygon(pen, points);e.Graphics.DrawPolygon(pen, points);points =points =newnew Point Point newnew Point(250,50), Point(250,50),newnew Point(300,100), Point(300,100),newnew Point(275,150), Point(2

71、75,150), newnew Point(225,150), Point(225,150),newnew Point(200,100) Point(200,100);g.FillPolygon(g.FillPolygon(newnew SolidBrush(Color.Red),points); SolidBrush(Color.Red),points); 373814.2.4曲线曲线 这里所讲的曲线是指自定义曲线,自定义曲线有两这里所讲的曲线是指自定义曲线,自定义曲线有两种形式:打开的曲线和封闭的曲线种形式:打开的曲线和封闭的曲线。在在Graphics类中,绘制自定义曲线的方法有:类中,绘

72、制自定义曲线的方法有: DrawCurve()方法方法 DrawClosedCurve()方法方法 以及应用广泛的绘制贝塞尔曲线的以及应用广泛的绘制贝塞尔曲线的 DrawBezier()方法方法DrawBeziers()方法。方法。391、DrawCurve()方法方法 这这个个方方法法用用光光滑滑的的曲曲线线把把给给定定的的点点连连接接起起来来,常常用用形形式有:式有: public void DrawCurve(Pen pen, Point points)public void DrawCurve(Pen pen, Point points) 其其中中,PointPoint结结构构类类型型

73、的的数数组组中中指指明明各各节节点点,默默认认弯曲强度为弯曲强度为0.50.5,注意数组中至少要有,注意数组中至少要有4 4个元素。个元素。 public public void void DrawCurve(Pen DrawCurve(Pen pen, pen, Point Point points, points, float tension)float tension) 其中其中tension指定弯曲强度,该值范围为指定弯曲强度,该值范围为0.0f1.0f,超出此范围会产生异常,当弯曲强度为零时,就是直,超出此范围会产生异常,当弯曲强度为零时,就是直线。线。40【例】绘制直线与平滑曲线。

74、【例】绘制直线与平滑曲线。privateprivate voidvoid Form1_Paint( Form1_Paint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e) Pen redPen = Pen redPen = newnew Pen(Color.Red, 3); Pen(Color.Red, 3);Pen greenPen = Pen greenPen = newnew Pen(Color.Green, 3); Pen

75、(Color.Green, 3);Point curvePoints =Point curvePoints = newnew Point( 50, 250), Point( 50, 250),newnew Point(100, 25), Point(100, 25),newnew Point(200, 250), Point(200, 250),newnew Point(250, 50), Point(250, 50),newnew Point(300, 75), Point(300, 75),newnew Point(350, 200), Point(350, 200),newnew Poi

76、nt(400, 150) Point(400, 150);e.Graphics.DrawLines(redPen, curvePoints);e.Graphics.DrawLines(redPen, curvePoints);e.Graphics.DrawCurve(greenPen, curvePoints);e.Graphics.DrawCurve(greenPen, curvePoints);41422、DrawClosedCurve()方法方法 这这个个方方法法也也是是用用平平滑滑的的曲曲线线将将各各节节点点连连接接起起来来,但但会会自自动动把把首首尾节点连接起来构成封闭曲线。尾节点连

77、接起来构成封闭曲线。3、贝塞尔曲线、贝塞尔曲线 每每段段贝贝塞塞尔尔曲曲线线都都需需要要四四个个点点,第第一一个个点点是是起起始始点点,第第四四个个点点是是终终止止点点,第第二二个个点点和和第第三三个个点点控控制制曲曲线线的的形形状状。使使用用DrawBezierDrawBezier()()方方法法绘绘制制一一段段贝贝塞塞尔尔曲曲线线,使使用用DrawBeziers()DrawBeziers()方方法法绘绘制制多多段段贝贝塞塞尔曲线。常用形式有:尔曲线。常用形式有: public public void void DrawBezier(Pen DrawBezier(Pen pen, pen,

78、Point Point pt1, pt1, Point Point pt2, pt2, Point pt3, Point pt4)Point pt3, Point pt4) 其中其中pt1pt1、pt2pt2、pt3pt3、pt4pt4分别指定四个点。分别指定四个点。 public void DrawBezier(Pen pen, Point points)public void DrawBezier(Pen pen, Point points) 其中其中points是是Point结构的数组,第一段贝塞尔曲线从点数组中的结构的数组,第一段贝塞尔曲线从点数组中的第一个点到第四个点绘制而成。以后每

79、段曲线只需要三个点:两个第一个点到第四个点绘制而成。以后每段曲线只需要三个点:两个控制点和一个结束点。前一段曲线的结束点会自动用作后一段曲线控制点和一个结束点。前一段曲线的结束点会自动用作后一段曲线的起始点。的起始点。43【例】绘制贝塞尔曲线。【例】绘制贝塞尔曲线。privateprivate voidvoid Form1_Paint( Form1_Paint(objectobject sender, sender, System.Windows.Forms.PaintEventArgs e) System.Windows.Forms.PaintEventArgs e) Pen blackPe

80、n = Pen blackPen = newnew Pen(Color.Black, 3); Pen(Color.Black, 3);Point bezierPoints =Point bezierPoints = newnew Point(50, 100), Point(50, 100),newnew Point(100, 10), Point(100, 10),newnew Point(150,290), Point(150,290),newnew Point(200, 100), Point(200, 100),newnew Point(250,10), Point(250,10),ne

81、wnew Point(300, 290), Point(300, 290),newnew Point(350,100) Point(350,100) ; ;e.Graphics.DrawBeziers(blackPen, bezierPoints);e.Graphics.DrawBeziers(blackPen, bezierPoints); 444514.2.5 14.2.5 椭圆椭圆 椭椭圆圆是是一一种种特特殊殊的的封封闭闭曲曲线线,GraphicsGraphics类类专专门门提提供供了了绘绘制制椭椭圆圆的的两两种种方方法法:DrawEllipse()DrawEllipse()方方法法和和

82、FillEllipse()FillEllipse()方方法法。常常用用形形式式有:有: public void DrawEllipse(Pen pen, Rectangle rect)public void DrawEllipse(Pen pen, Rectangle rect) 其中其中rectrect为为RectangleRectangle结构,用于确定椭圆的边界。结构,用于确定椭圆的边界。 public public void void DrawEllipse(Pen DrawEllipse(Pen pen, pen, int int x, x, int int y, y, int in

83、t width, width, int height)int height) 其中其中x, yx, y为椭圆左上角的坐标,为椭圆左上角的坐标,widthwidth定义椭圆的边框的宽度定义椭圆的边框的宽度,height,height定义椭圆的边框的高度。定义椭圆的边框的高度。 public void FillEllipse(Pen pen, Rectangle rect)public void FillEllipse(Pen pen, Rectangle rect) 填填充充椭椭圆圆的的内内部部区区域域。其其中中rectrect为为RectangleRectangle结结构构,用用于于确确定定椭

84、椭圆的边界。圆的边界。 public public void void FillEllipse(Pen FillEllipse(Pen pen, pen, int int x, x, int int y, y, int int width, width, int height)int height) 填充椭圆的内部区域。其中填充椭圆的内部区域。其中x,y为椭圆左上角的坐标,为椭圆左上角的坐标,width定定义椭圆的边框的宽度义椭圆的边框的宽度,height定义椭圆的边框的高度。定义椭圆的边框的高度。4614.3图像的显示与保存图像的显示与保存14.3.1显示图像显示图像 可可以以使使用用 GD

85、I+ GDI+ 显显示示以以文文件件形形式式存存在在的的图图像像。图图像像文文件件可可以是以是BMPBMP、JPEGJPEG、GIFGIF、TIFFTIFF、PNGPNG等。实现步骤为:等。实现步骤为: 创建一个创建一个BitmapBitmap对象,指明要显示的图像文件;对象,指明要显示的图像文件; 创建一个创建一个GraphicsGraphics对象,表示要使用的绘图平面;对象,表示要使用的绘图平面; 调用调用 Graphics Graphics 对象的对象的 DrawImage DrawImage 方法显示图像。方法显示图像。 创建创建BitmapBitmap对象对象 Bitmap Bit

86、map类有很多重载的构造函数,其中之一是:类有很多重载的构造函数,其中之一是: Public Bitmap(string filename) Public Bitmap(string filename) 可以利用该构造函数创建可以利用该构造函数创建BitmapBitmap对象,例如:对象,例如: Bitmap bitmap = new Bitmap(“tu1.jpg”); Bitmap bitmap = new Bitmap(“tu1.jpg”); 47 DrawImage() DrawImage()方法方法 GraphicsGraphics类类的的DrawImage()DrawImage()

87、方方法法用用于于在在指指定定位位置置显显示示原原始始图图像像或或者者缩缩放放后后的的图图像像。该该方方法法的的重重载载形形式式非非常常多多,其中之一为:其中之一为: public public void void DrawImage(Image DrawImage(Image image, image, int int x, x, int int y, y, int int width, width, int height)int height) 该方法在该方法在x,y按指定的大小显示图像。利用这个方法按指定的大小显示图像。利用这个方法可以直接显示缩放后的图像。可以直接显示缩放后的图像。【例例

88、】假假设设窗窗体体中中有有一一个个ButtonButton按按钮钮button1button1,可可以以在在单单击击按按钮钮的的事事件件代码中显示图像。代码中显示图像。privateprivate voidvoid button1_Click( button1_Click(objectobject sender, System.EventArgs e) sender, System.EventArgs e) Bitmap bitmap = Bitmap bitmap = newnew Bitmap(d:testtu1.jpg); Bitmap(d:testtu1.jpg);Graphics g

89、 = Graphics g = thisthis.CreateGraphics();.CreateGraphics();g.DrawImage(bitmap,3,10,200,200);g.DrawImage(bitmap,3,10,200,200);g.DrawImage(bitmap,250,10,50,50);g.DrawImage(bitmap,250,10,50,50);g.DrawImage(bitmap,350,10,bitmap.Width/2, bitmap.Height/2);g.DrawImage(bitmap,350,10,bitmap.Width/2, bitmap.

90、Height/2); 484914.3.2保存图像保存图像 使使用用画画图图功功能能在在窗窗体体上上绘绘制制出出图图形形或或者者图图像像后后,可可以以多种格式保存到图像文件中以以多种格式保存到图像文件中. . 下面的例子说明了具体的用法。下面的例子说明了具体的用法。【例】将绘制的图形或图像保存到文件中。【例】将绘制的图形或图像保存到文件中。 创建一个创建一个Windows应用程序,设计画面:应用程序,设计画面:50 添加名称空间引用添加名称空间引用usingusing System.Drawing.Drawing2D; System.Drawing.Drawing2D; 添加【画图】按钮的添加

91、【画图】按钮的ClickClick事件代码事件代码privateprivate voidvoid button1_Click( button1_Click(objectobject sender, System.EventArgs e) sender, System.EventArgs e) Graphics g = Graphics g = thisthis.CreateGraphics();.CreateGraphics();DrawMyImage(g);DrawMyImage(g); 添加调用的方法添加调用的方法privateprivate voidvoid DrawMyImage(Gr

92、aphics g) DrawMyImage(Graphics g) Rectangle rect1=Rectangle rect1=newnew Rectangle(0,0, Rectangle(0,0,thisthis.Width/4,.Width/4, this this.Height-100);.Height-100);HatchBrush hatchBrush = HatchBrush hatchBrush = newnew HatchBrush(HatchStyle.Shingle, HatchBrush(HatchStyle.Shingle, Color.White, Color.

93、Black); Color.White, Color.Black);g.FillEllipse(hatchBrush, rect1);g.FillEllipse(hatchBrush, rect1);Rectangle rect2=Rectangle rect2=newnew Rectangle( Rectangle(thisthis.Width/4+50,0,.Width/4+50,0, thisthis.Width/4,.Width/4,thisthis.Height-100);.Height-100);51hatchBrush = hatchBrush = newnew HatchBru

94、sh(HatchStyle.WideUpwardDiagonal, HatchBrush(HatchStyle.WideUpwardDiagonal, Color.White, Color.Red);Color.White, Color.Red);g.FillRectangle(hatchBrush,rect2);g.FillRectangle(hatchBrush,rect2);intint x= x=thisthis.Width-50-.Width-50-thisthis.Width/4;.Width/4;Point points =Point points =newnew Point P

95、oint newnew Point(x,10), Point(x,10),newnew Point(x+50,60), Point(x+50,60),newnew Point(x+150,10), Point(x+150,10),newnew Point(x+200,160), Point(x+200,160),newnew Point(x+150,260), Point(x+150,260),newnew Point(x+50,260), Point(x+50,260),newnew Point(x,160); Point(x,160);hatchBrush = hatchBrush = n

96、ewnew HatchBrush(HatchStyle.SmallConfetti, HatchBrush(HatchStyle.SmallConfetti, Color.White, Color.Red); Color.White, Color.Red);TextureBrush myBrush = TextureBrush myBrush = new new TextureBrush( TextureBrush(newnew Bitmap(e:testm23.jpg); Bitmap(e:testm23.jpg);g.FillClosedCurve(myBrush,points);g.Fi

97、llClosedCurve(myBrush,points); 52 添加【保存】按钮的添加【保存】按钮的ClickClick事件代码事件代码privateprivate voidvoid button2_Click( button2_Click(objectobject sender, System.EventArgs e) sender, System.EventArgs e) /构造一个指定区域的空图像构造一个指定区域的空图像Bitmap image=Bitmap image=newnew Bitmap( Bitmap(thisthis.Width,.Width,thisthis.Heig

98、ht-100);.Height-100);/根据指定区域得到根据指定区域得到GraphicsGraphics对象对象Graphics g=Graphics.FromImage(image);Graphics g=Graphics.FromImage(image);/设置图像的背景色设置图像的背景色g.Clear(g.Clear(thisthis.BackColor);.BackColor);/将图形画到将图形画到GraphicsGraphics对象中对象中DrawMyImage(g);DrawMyImage(g);trytry /保存画到保存画到GraphicsGraphics对象中的图形对象

99、中的图形image.Save(d:testtu1.jpg,System.Drawing.Imaging.ImageFimage.Save(d:testtu1.jpg,System.Drawing.Imaging.ImageFormat.Jpeg);ormat.Jpeg);53g=g=thisthis.CreateGraphics();.CreateGraphics();Rectangle rect=Rectangle rect=newnew Rectangle(0,0, Rectangle(0,0,thisthis.Width,.Width,thisthis.Height-.Height-10

100、0);100);g.FillRectangle(g.FillRectangle(newnew SolidBrush( SolidBrush(thisthis.BackColor),rect);.BackColor),rect);MessageBox.Show(MessageBox.Show(保存成功!保存成功!,恭喜恭喜););catchcatch(Exception err)(Exception err)MessageBox.Show(err.Message);MessageBox.Show(err.Message); 添加【显示】按钮的添加【显示】按钮的ClickClick事件代码事件代码

101、privateprivate voidvoid button3_Click( button3_Click(objectobject sender, System.EventArgs e) sender, System.EventArgs e) Rectangle rect=Rectangle rect=newnew Rectangle(0,0, Rectangle(0,0,thisthis.Width,.Width,thisthis.Height-.Height-100);100);Graphics g=Graphics g=thisthis.CreateGraphics();.CreateG

102、raphics();Image image=Image image=newnew Bitmap(d:testtu1.jpg); Bitmap(d:testtu1.jpg);g.DrawImage(image,rect);g.DrawImage(image,rect); 545514.4动画设计动画设计14.4.1奔跑的豹子奔跑的豹子 分分别别设设计计豹豹子子奔奔跑跑的的八八种种不不同同形形状状,得得到到八八个个图图像像,保保存存到到文文件件名名为为t1.jpgt1.jpg到到t8.jpgt8.jpg中中。设设计计3 3只只豹豹子子从从左左向向右奔跑的动画,要求能随时调整奔跑的速度。右奔跑的动画

103、,要求能随时调整奔跑的速度。 1) 1)创建一个创建一个WindowsWindows应用程序,设置窗体的背景色为应用程序,设置窗体的背景色为白色;向设计窗体拖放三个【白色;向设计窗体拖放三个【PicturePoxPicturePox】控件,【】控件,【SizeModeSizeMode】属性均为【】属性均为【StretchImageStretchImage】,调整三只豹子】,调整三只豹子的大小使其看起来有立体感;一个的大小使其看起来有立体感;一个TrackBarTrackBar控件,两个控件,两个LabelLabel控件,控件,1 1个个TimerTimer控件。如图:控件。如图:5657源程序

104、如下:源程序如下:usingusing System.Drawing.Drawing2D; System.Drawing.Drawing2D;namespacenamespace Donghua Donghuapublicpublic classclass Form1 : System.Windows.Forms.Form Form1 : System.Windows.Forms.Form Bitmap bitmap;Bitmap bitmap;intint num; num;privateprivate System.Windows.Forms.TrackBar trackBar1; Sys

105、tem.Windows.Forms.TrackBar trackBar1;privateprivate System.Windows.Forms.Timer timer1; System.Windows.Forms.Timer timer1;privateprivate System.Windows.Forms.Label label1; System.Windows.Forms.Label label1;privateprivate System.Windows.Forms.Label label2; System.Windows.Forms.Label label2;privatepriv

106、ate System.Windows.Forms.PictureBox pictureBox1; System.Windows.Forms.PictureBox pictureBox1;privateprivate System.Windows.Forms.PictureBox pictureBox2; System.Windows.Forms.PictureBox pictureBox2;privateprivate System.Windows.Forms.PictureBox pictureBox3; System.Windows.Forms.PictureBox pictureBox3

107、;privateprivate System.ComponentModel.IContainer components; System.ComponentModel.IContainer components;publicpublic Form1() Form1() InitializeComponent();InitializeComponent();58thisthis.trackBar1.Minimum=10;.trackBar1.Minimum=10;thisthis.trackBar1.Maximum=100;.trackBar1.Maximum=100;thisthis.track

108、Bar1.Value=50;.trackBar1.Value=50;thisthis.timer1.Interval=.timer1.Interval=thisthis.trackBar1.Value;.trackBar1.Value;bitmap=bitmap=newnew Bitmap8; Bitmap8;forfor( (intint i=1;i=8;i+) i=1;i=8;i+) bitmapi-1=bitmapi-1=newnew Bitmap(d:testt+i.ToString()+.jpg); Bitmap(d:testt+i.ToString()+.jpg); num=0;n

109、um=0;thisthis.timer1.Enabled=.timer1.Enabled=truetrue; ; privateprivate voidvoid trackBar1_Scroll( trackBar1_Scroll(objectobject sender, System.EventArgs e) sender, System.EventArgs e) thisthis.timer1.Interval=.timer1.Interval=thisthis.trackBar1.Value;.trackBar1.Value; 59privateprivate voidvoid time

110、r1_Tick( timer1_Tick(objectobject sender, System.EventArgs e) sender, System.EventArgs e) ifif(num8)(num700).pictureBox1.Left700) thisthis.pictureBox1.Left=-100;.pictureBox1.Left=-100;thisthis.pictureBox2.Left=.pictureBox2.Left=thisthis.pictureBox1.Left-20;.pictureBox1.Left-20;thisthis.pictureBox3.L

111、eft=.pictureBox3.Left=thisthis.pictureBox2.Left-20;.pictureBox2.Left-20; 60elseelse thisthis.pictureBox1.Left+=10;.pictureBox1.Left+=10;thisthis.pictureBox2.Left=.pictureBox2.Left=thisthis.pictureBox1.Left-20;.pictureBox1.Left-20;thisthis.pictureBox3.Left=.pictureBox3.Left=thisthis.pictureBox2.Left-

112、20;.pictureBox2.Left-20; 3)3)运行,观察效果。运行,观察效果。6114.4.2 14.4.2 图像变换图像变换 创建一个创建一个WindowsWindows应用程序,向窗体拖放应用程序,向窗体拖放1 1个个PictureBoxPictureBox控件,黑色背景;控件,黑色背景;1 1个个ListBoxListBox控件,控件,5 5号字;号字;2 2个个ButtonButton控件。设计画面:控件。设计画面:62 源程序如下:源程序如下:usingusing System.Drawing.Drawing2D; System.Drawing.Drawing2D;nam

113、espacenamespace WinTest WinTest publicpublic classclass Form1 : System.Windows.Forms.Form Form1 : System.Windows.Forms.Form Bitmap myBitmap;Bitmap myBitmap;publicpublic Form1() Form1() InitializeComponent();InitializeComponent();stringstring items= items= 左到右拉伸左到右拉伸, 上到下拉伸上到下拉伸, 中间到四周扩散中间到四周扩散, 反转反转

114、, 中间向两边拉伸中间向两边拉伸,63 上下对接上下对接, 左右对接左右对接 ;thisthis.listBox1.Items.AddRange(items);.listBox1.Items.AddRange(items);thisthis.listBox1.SelectedIndex=0;.listBox1.SelectedIndex=0;thisthis.buttonStart.Enabled=.buttonStart.Enabled=falsefalse; ;thisthis.listBox1.Enabled=.listBox1.Enabled=falsefalse; ; private

115、private voidvoid buttonStart_Click( buttonStart_Click(objectobject sender, sender, System.EventArgs e) System.EventArgs e) stringstring str= str=thisthis.listBox1.SelectedItem.ToString();.listBox1.SelectedItem.ToString(); intint width= width=thisthis.pictureBox1.Width; .pictureBox1.Width; /图像宽度图像宽度

116、intint height= height=thisthis.pictureBox1.Height; .pictureBox1.Height; /图像高度图像高度 /为在为在PictureBoxPictureBox中画图作准备中画图作准备 Graphics g= Graphics g=thisthis.pictureBox1.CreateGraphics();.pictureBox1.CreateGraphics();64g.Clear(Color.Black); g.Clear(Color.Black); /初始为全黑色初始为全黑色switchswitch(str)(str) casecas

117、e 左到右拉伸左到右拉伸: forfor( (intint x=0;x=width;x+) x=0;x=width;x+) g.DrawImage(myBitmap,0,0,x,height);g.DrawImage(myBitmap,0,0,x,height); breakbreak; ; casecase 上到下拉伸上到下拉伸: forfor( (intint y=0;y=height;y+) y=0;y=height;y+) g.DrawImage(myBitmap,0,0,width,y);g.DrawImage(myBitmap,0,0,width,y); breakbreak; ;

118、 65casecase “ “中间到四周扩散中间到四周扩散”:”: forfor( (intint x=0;x=width/2;x+) x=0;x=width/2;x+) Rectangle destRect= Rectangle destRect=newnew Rectangle(width/2-x, Rectangle(width/2-x, height/2-x,2*x,2*x); height/2-x,2*x,2*x); Rectangle srcRect= Rectangle srcRect=newnew Rectangle(0,0, Rectangle(0,0, myBitmap.W

119、idth,myBitmap.Height); myBitmap.Width,myBitmap.Height); g.DrawImage(myBitmap,destRect,srcRect, g.DrawImage(myBitmap,destRect,srcRect, GraphicsUnit.Pixel); GraphicsUnit.Pixel); breakbreak;casecase “ “反转反转”:”: forfor( (intint x=-width/2;x=width/2;x+) x=-width/2;x=width/2;x+) Rectangle destRect=Rectang

120、le destRect=newnew Rectangle(0,height/2-x, Rectangle(0,height/2-x, width,2*x); width,2*x);66Rectangle srcRect=Rectangle srcRect=newnew Rectangle(0,0, Rectangle(0,0, myBitmap.Width,myBitmap.Height); myBitmap.Width,myBitmap.Height);g.DrawImage(myBitmap,destRect,srcRect,g.DrawImage(myBitmap,destRect,sr

121、cRect, GraphicsUnit.Pixel); GraphicsUnit.Pixel); breakbreak; ; case case 中间向两边拉伸中间向两边拉伸: forfor( (intint y=0;y=width/2;y+) y=0;y=width/2;y+) Rectangle destRect= Rectangle destRect=newnew Rectangle(width/2-y,0, Rectangle(width/2-y,0, 2*y,height); 2*y,height); Rectangle srcRect= Rectangle srcRect=newn

122、ew Rectangle(0,0, Rectangle(0,0, myBitmap.Width,myBitmap.Height); myBitmap.Width,myBitmap.Height); g.DrawImage(myBitmap,destRect,srcRect, g.DrawImage(myBitmap,destRect,srcRect, GraphicsUnit.Pixel); GraphicsUnit.Pixel); breakbreak; ; 67casecase “ “上下对接上下对接”:”: Bitmap bitmap= Bitmap bitmap=newnew Bitm

123、ap(width,height); Bitmap(width,height); intint x=0; x=0; whilewhile(x=height/2)(x=height/2) forfor( (intint i=0;i=width-1;i+) i=0;i=width-1;i+) bitmap.SetPixel(i,x, bitmap.SetPixel(i,x, myBitmap.GetPixel(i,x); myBitmap.GetPixel(i,x); 68forfor( (intint i=0;i=width-1;i+) i=0;i=width-1;i+) bitmap.SetPi

124、xel(i,height-x-1, bitmap.SetPixel(i,height-x-1, myBitmap.GetPixel(i,height-x-1); myBitmap.GetPixel(i,height-x-1); x+;x+;thisthis.pictureBox1.Refresh();.pictureBox1.Refresh();thisthis.pictureBox1.Image=bitmap;.pictureBox1.Image=bitmap; breakbreak; case case “ “左右对接左右对接”:”: Bitmap bitmap= Bitmap bitma

125、p=newnew Bitmap(width,height); Bitmap(width,height); intint x=0; x=0; whilewhile(x=width/2)(x=width/2) forfor( (intint i=0;i=height-1;i+) i=0;i=height-1;i+) bitmap.SetPixel(x,i,myBitmap.GetPixel(x,i);bitmap.SetPixel(x,i,myBitmap.GetPixel(x,i); 69forfor( (intint i=0;i=height-1;i+) i=0;i=height-1;i+)

126、bitmap.SetPixel(width-x-1,i,bitmap.SetPixel(width-x-1,i, myBitmap.GetPixel(width-x-1,i); myBitmap.GetPixel(width-x-1,i); x+;x+;thisthis.pictureBox1.Refresh();.pictureBox1.Refresh();thisthis.pictureBox1.Image=bitmap;.pictureBox1.Image=bitmap; breakbreak;privateprivate voidvoid buttonSelectFile_Click(

127、 buttonSelectFile_Click(objectobject sender, sender, System.EventArgs e)System.EventArgs e) OpenFileDialog openFile=OpenFileDialog openFile=newnew OpenFileDialog(); OpenFileDialog();openFile.Filter=*.jpg;*.bmp;*.*|*.jpg;*.bmp;*.*;openFile.Filter=*.jpg;*.bmp;*.*|*.jpg;*.bmp;*.*;ifif(openFile.ShowDial

128、og()=DialogResult.OK)(openFile.ShowDialog()=DialogResult.OK) fileName=openFile.FileName;fileName=openFile.FileName;70/得到原始大小的图像得到原始大小的图像Bitmap srcBitmap=Bitmap srcBitmap=newnew Bitmap(fileName); Bitmap(fileName);/得到缩放后的图像得到缩放后的图像myBitmap=myBitmap=newnew Bitmap(srcBitmap, Bitmap(srcBitmap, thisthis.p

129、ictureBox1.Width,.pictureBox1.Width,thisthis.pictureBox1.Height);.pictureBox1.Height);thisthis.pictureBox1.Image=myBitmap;.pictureBox1.Image=myBitmap;thisthis.buttonStart.Enabled=.buttonStart.Enabled=truetrue; ;thisthis.listBox1.Enabled=.listBox1.Enabled=truetrue; ; 运运行行。选选择择一一个个图图像像文文件件,即即得得到到如如图图1

130、7-117-1所所示示效效果果,从从列列表表框框中中选选择择一一个个显显示示方方式式,单单击击【开开始始】按按钮钮观观察察效果。效果。717214.5Web应用程序中的图形图像操作应用程序中的图形图像操作 在在WebWeb应应用用程程序序中中,图图形形图图像像操操作作的的方方式式和和WindowsWindows应应用用程程序序基基本本相相同同。实实际际上上,对对笔笔、画画笔笔以以及及BitmapBitmap对对象象、GraphicsGraphics对对象象的的介介绍绍同同样样适适用用于于WebWeb应应用用程程序序,只只是是显显示示方方式式有有些些区区别别,本本节节主主要要介介绍绍WebWeb

131、应应用用程程序序中中画画图图的的方法以及显示图像的方法。方法以及显示图像的方法。17.5.1绘制图形绘制图形 在在Web应用程序中,首先将应用程序中,首先将Response.ContentType属属性设置为性设置为“image/gif”以便让浏览器正确的解释响应,以便让浏览器正确的解释响应,然后使用然后使用17.1到到17.3介绍的方法绘制图形,最后利用介绍的方法绘制图形,最后利用Image对象的对象的Save()方法将图像输出到客户浏览器。方法将图像输出到客户浏览器。73【例】在【例】在WebWeb应用程序中绘制贝塞尔曲线。应用程序中绘制贝塞尔曲线。 新新建建一一个个WebWeb应应用用程

132、程序序,切切换换到到HTMLHTML方方式式,在在间间加加入入。 切切换换到到设设计计视视图图,调调整整iframeiframe控控件件到到合合适适大大小小,鼠鼠标标右右击击该该控控件件,选选择择“作为服务器控件运行作为服务器控件运行”。 在设计窗体中添加一个按钮,加入该按钮的在设计窗体中添加一个按钮,加入该按钮的ClickClick事件代码事件代码privateprivate voidvoid Button1_Click( Button1_Click(objectobject sender, System.EventArgs e) sender, System.EventArgs e) th

133、isthis.IFRAME1.Attributessrc=WebForm2.aspx;.IFRAME1.Attributessrc=WebForm2.aspx; 添添加加一一个个新新窗窗体体WebForm2.aspxWebForm2.aspx,在在WebForm2.aspxWebForm2.aspx窗窗体体的的Page_loadPage_load中加入代码中加入代码privateprivate voidvoid Page_Load( Page_Load(objectobject sender, System.EventArgs e) sender, System.EventArgs e) if

134、if(!(!thisthis.IsPostBack).IsPostBack)Pen blackPen = Pen blackPen = newnew Pen(Color.Black, 3); Pen(Color.Black, 3);74Point bezierPoints =Point bezierPoints =newnew Point(50, 100), Point(50, 100),newnew Point(100, 10), Point(100, 10),newnew Point(150,290), Point(150,290),newnew Point(200, 100), Poin

135、t(200, 100),newnew Point(250,10), Point(250,10),newnew Point(300, 290), Point(300, 290),newnew Point(350,100); Point(350,100);intint width=400; width=400; intint height=300; height=300;Bitmap bitmap=Bitmap bitmap=newnew Bitmap(width,height); Bitmap(width,height);Graphics g=Graphics.FromImage(bitmap)

136、;Graphics g=Graphics.FromImage(bitmap);g.Clear(Color.White);g.Clear(Color.White);g.DrawBeziers(blackPen, bezierPoints);g.DrawBeziers(blackPen, bezierPoints);Response.ContentType=image/gif;Response.ContentType=image/gif;75bitmap.Save(Response.OutputStream,bitmap.Save(Response.OutputStream, System.Dra

137、wing.Imaging.ImageFormat.Gif); System.Drawing.Imaging.ImageFormat.Gif); 运行,结果如图运行,结果如图17-117-1。7614.5.2Web图片浏览器图片浏览器 利利用用GDI+GDI+作作图图功功能能,可可以以设设计计出出具具有有漂漂亮亮外外观观的的图图片浏览器,下面用一个简单的例子说明具体的实现方法。片浏览器,下面用一个简单的例子说明具体的实现方法。【例】【例】WebWeb图片浏览器示例。图片浏览器示例。 新新 建建 一一 个个 WebWeb应应 用用 程程 序序 , 切切 换换 到到 HTMLHTML方方 式式 ,

138、在在间加入间加入。 切切换换到到设设计计视视图图,调调整整iframeiframe控控件件到到合合适适大大小小,鼠鼠标右击该控件,选择标右击该控件,选择“作为服务器控件运行作为服务器控件运行”。 向设计界面添加三个向设计界面添加三个ButtonButton控件,一个控件,一个LabelLabel控件,控件,如图如图17-117-1:7778 在在Page_loadPage_load中添加初始化代码中添加初始化代码privateprivate voidvoid Page_Load( Page_Load(objectobject sender, System.EventArgs e) sender

139、, System.EventArgs e) / / 在此处放置用户代码以初始化页面在此处放置用户代码以初始化页面ifif(!(!thisthis.IsPostBack).IsPostBack) Sessionindex=0; Sessionindex=0; thisthis.ButtonPosition.Text=;.ButtonPosition.Text=; 添加【上一个】按钮的添加【上一个】按钮的ClickClick事件代码事件代码privateprivate voidvoid Button1_Click( Button1_Click(objectobject sender, System

140、.EventArgs e) sender, System.EventArgs e) intint index=( index=(intint)Sessionindex;)Sessionindex;ifif(index1)(index1) index-; index-;thisthis.ButtonPosition.Text=index.ToString()+ of 20;.ButtonPosition.Text=index.ToString()+ of 20;Sessionindex=index;Sessionindex=index;79Sessionfilename=Server.MapPa

141、th(.)+picture+Sessionfilename=Server.MapPath(.)+picture+ index.ToString()+.jpg; index.ToString()+.jpg;thisthis.IFRAME1.Attributessrc=WebForm4.aspx;.IFRAME1.Attributessrc=WebForm4.aspx; 添加【下一个】按钮的添加【下一个】按钮的ClickClick事件代码事件代码privateprivate voidvoid Button2_Click( Button2_Click(objectobject sender, Sys

142、tem.EventArgs e) sender, System.EventArgs e) intint index=( index=(intint)Sessionindex;)Sessionindex;ifif(index20)(index20) index+;index+;thisthis.ButtonPosition.Text=index.ToString()+ of 20;.ButtonPosition.Text=index.ToString()+ of 20;Sessionindex=index;Sessionindex=index; Sessionfilename=Server.Ma

143、pPath(.)+picture“Sessionfilename=Server.MapPath(.)+picture“ +index.ToString()+.jpg; +index.ToString()+.jpg;80thisthis.IFRAME1.Attributessrc=WebForm4.aspx;.IFRAME1.Attributessrc=WebForm4.aspx; 添添加加一一个个新新窗窗体体WebForm2.aspxWebForm2.aspx,在在WebForm2.aspxWebForm2.aspx窗窗体体的的Page_loadPage_load中加入代码中加入代码priva

144、teprivate voidvoid Page_Load( Page_Load(objectobject sender, System.EventArgs e) sender, System.EventArgs e) / / 在此处放置用户代码以初始化页面在此处放置用户代码以初始化页面ifif(!(!thisthis.IsPostBack).IsPostBack) /得到要显示的图像文件名得到要显示的图像文件名stringstring filename=( filename=(stringstring)Sessionfilename;)Sessionfilename;/根据文件名创建原始大小的

145、根据文件名创建原始大小的BitmapBitmap对象对象Bitmap bitmap=Bitmap bitmap=newnew Bitmap(filename); Bitmap(filename);/将其缩放到将其缩放到200200200200bitmap=bitmap=newnew Bitmap(bitmap,200,200); Bitmap(bitmap,200,200);/根据缩放后的根据缩放后的BitmapBitmap对象创建画笔对象创建画笔TextureBrush myBrush = TextureBrush myBrush = newnew TextureBrush(bitmap);

146、 TextureBrush(bitmap);81/重新创建重新创建200200像素宽、像素宽、300300像素高的空像素高的空BitmapBitmap对象对象bitmap=bitmap=newnew Bitmap(300,200); Bitmap(300,200);Graphics g=Graphics.FromImage(bitmap);Graphics g=Graphics.FromImage(bitmap);/设置清除绘图平面原有内容,并设置画图平面背景色为白色设置清除绘图平面原有内容,并设置画图平面背景色为白色g.Clear(Color.White);g.Clear(Color.Whi

147、te);/填充椭圆填充椭圆g.FillEllipse(myBrush,0,0,300,200);g.FillEllipse(myBrush,0,0,300,200);/设置输出类型设置输出类型Response.ContentType=image/gif;Response.ContentType=image/gif;/输出到客户浏览器输出到客户浏览器bitmap.Save(Response.OutputStream,System.Drawing.Imagibitmap.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gi

148、f);ng.ImageFormat.Gif); 运行,效果如图运行,效果如图828314.6音频与视频播放音频与视频播放 在在C# C# 默默认认的的【工工具具箱箱】中中,并并没没有有直直接接支支持持播播放放音音频频/ /视视频频文文件件的的控控件件或或组组件件,但但是是在在C# C# 中中可可以以直直接接调调用用COMCOM组组件件,而而且且使使用用也也非非常常简简单单。在在COMCOM组组件件中中,有有一一个个【Windows Windows Media Media PlayerPlayer】控控件件,我我们们可可以以利利用用这这个个控控件件实实现现媒媒体体播播放放功能。功能。17.6.1

149、WindowsMediaPlayer控件控件 要要使使用用【Windows Windows Media Media PlayerPlayer】控控件件,需需要要先先将将其其从从COMCOM组件添加到【工具箱】中。具体步骤如下:组件添加到【工具箱】中。具体步骤如下:创建一个创建一个Windows应用程序,在设计视图下,选择应用程序,在设计视图下,选择【工具箱】,然后鼠标右键单击【我的用户控件】,选择【工具箱】,然后鼠标右键单击【我的用户控件】,选择【添加添加/移除项】,此时即弹出自定义工具箱窗口移除项】,此时即弹出自定义工具箱窗口,选择【,选择【COM组件】组件】【WindowsMediaPla

150、yer】,如图:】,如图:8485 勾勾选选【Windows Windows Media Media PlayerPlayer】,单单击击【确确定定】。这这样样,就就把把该该控控件添加到工具箱【我的用户控件】中了。件添加到工具箱【我的用户控件】中了。14.6.2制作媒体播放器制作媒体播放器 Windows Media Player Windows Media Player控件常用属性、事件和方法有:控件常用属性、事件和方法有: FileNameFileName属性:播放的文件。例如:属性:播放的文件。例如: this this.axMediaPlayer1.FileName=test.mpeg

151、;.axMediaPlayer1.FileName=test.mpeg; Display Display属性:显示画面大小,例如:属性:显示画面大小,例如: this this.axMediaPlayer1.DisplaySize=.axMediaPlayer1.DisplaySize= MediaPlayer.MPDisplaySizeConstants.mpFullScreen; MediaPlayer.MPDisplaySizeConstants.mpFullScreen; AutoStart AutoStart属性:是否自动播放。例如:属性:是否自动播放。例如: thisthis.ax

152、MediaPlayer1.AutoStart=.axMediaPlayer1.AutoStart=falsefalse; ; 如果要实现多个文件的连续播放,可以利用如果要实现多个文件的连续播放,可以利用EndOfStream事件,事件,该事件在播放当前文件结束时触发,比如将播放的多个文件事先放该事件在播放当前文件结束时触发,比如将播放的多个文件事先放到一个到一个ListBox中,然后在该事件代码中指定新的播放文件选项等。中,然后在该事件代码中指定新的播放文件选项等。86【例】简单媒体播放器。【例】简单媒体播放器。 在上面创建的在上面创建的Windows应用程序中,将应用程序中,将Windows

153、MediaPlayer从【我的用户控件】拖放到设计窗体上。然从【我的用户控件】拖放到设计窗体上。然后向窗体拖放一个后向窗体拖放一个Button控件,界面如图:控件,界面如图:87 为为了了让让播播放放画画面面随随窗窗口口大大小小改改变变,可可以以将将【Windows Windows Media Media PlayerPlayer】控控件件的的【AnchorAnchor】属属性性设设置置为为TopTop、LeftLeft、BottomBottom、RightRight,即让其四方都锚定。,即让其四方都锚定。 添加添加“打开打开”按钮的按钮的ClickClick事件代码事件代码privatepr

154、ivate voidvoid button1_Click( button1_Click(objectobject sender, System.EventArgs e) sender, System.EventArgs e) OpenFileDialog myFile = OpenFileDialog myFile = newnew OpenFileDialog(); OpenFileDialog();/过滤掉其它类型的文件过滤掉其它类型的文件myFile.Filter =*.mp3;*.wav;*.mpeg;*.avi;*.wmvmyFile.Filter =*.mp3;*.wav;*.mp

155、eg;*.avi;*.wmv |*.mp3;*.wav;*.mpeg;*.avi;*.wmv; |*.mp3;*.wav;*.mpeg;*.avi;*.wmv;/检查文件和路径是否存在检查文件和路径是否存在myFile.CheckFileExists = myFile.CheckFileExists = truetrue; ;myFile.CheckPathExists = myFile.CheckPathExists = truetrue; ;ifif(myFile.ShowDialog() = DialogResult.OK)(myFile.ShowDialog() = DialogRes

156、ult.OK) thisthis.axMediaPlayer1.FileName=myFile.FileName;.axMediaPlayer1.FileName=myFile.FileName; 运行,运行,单击“打开打开”按按钮,选择要播放的文件,就可以听到或看到播要播放的文件,就可以听到或看到播放的内容。放的内容。88 这个例子只是简单说明了该控件的用法,在实际应用中,可以这个例子只是简单说明了该控件的用法,在实际应用中,可以根据需要设计比较实用的画面。如果只是为了播放音频文件,可以根据需要设计比较实用的画面。如果只是为了播放音频文件,可以将该控件的外观改为图将该控件的外观改为图17-1所示的形式,并设置【所示的形式,并设置【AllowChangeDisplaySize】属性为【】属性为【false】。】。 也可以将该控件的【也可以将该控件的【Visible】属性设置为【】属性设置为【false】不让播放控】不让播放控制条显示出来。当然,这样做需要在代码中直接控制播放、暂停、制条显示出来。当然,这样做需要在代码中直接控制播放、暂停、停止、快进、快退等功能,具体使用方法请读者自己尝试。停止、快进、快退等功能,具体使用方法请读者自己尝试。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号