圖形圖像與多媒體編程_第1頁(yè)
圖形圖像與多媒體編程_第2頁(yè)
圖形圖像與多媒體編程_第3頁(yè)
圖形圖像與多媒體編程_第4頁(yè)
圖形圖像與多媒體編程_第5頁(yè)
已閱讀5頁(yè),還剩84頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、第十四章 圖形圖像與多媒體編程 14.1 GDI+概述 14.2 繪制(huzh)圖形 14.3 圖像的顯示與保存 14.4 動(dòng)畫(huà)設(shè)計(jì)14.5 Web應(yīng)用程序中的圖形圖像操作14.6 音頻與視頻播放共八十九頁(yè)14.1 GDI+概述(i sh) GDI+:Graphics Device Interface Plus,它提供了各種豐富的圖形圖像處理功能在C#.NET中,使用GDI+處理二維(2D)的圖形和圖像,使用DirectX處理三維(3D)的圖形圖像。 GDI+主要有“二維矢量圖形”、“圖像處理”和“版式(bnsh)”三部分組成。 GDI+提供了存儲(chǔ)基元自身相關(guān)信息的類(lèi)和結(jié)構(gòu)、存儲(chǔ)基元繪制方式

2、相關(guān)信息的類(lèi),以及實(shí)際進(jìn)行繪制的類(lèi)。 GDI+ 為使用各種字體、字號(hào)和樣式來(lái)顯示文本這種復(fù)雜任務(wù)提供了大量的支持。 其他高級(jí)功能共八十九頁(yè)在C#中,所有圖形圖像處理功能都包含在以下名稱(chēng)空間下: 1. System.Drawing名稱(chēng)空間 提供了對(duì)GDI+基本圖形功能的訪(fǎng)問(wèn),主要有Graphics類(lèi)、Bitmap類(lèi)、從Brush類(lèi)繼承(jchng)的類(lèi)、Font類(lèi)、Icon類(lèi)、Image類(lèi)、Pen類(lèi)、Color類(lèi)等。2. System.Drawing.Drawing2D名稱(chēng)空間 提供了高級(jí)的二維和矢量圖形功能。主要有梯度型畫(huà)刷、Matrix類(lèi)(用于定義幾何變換)和GraphicsPath類(lèi)等。

3、3. System.Drawing.Imaging名稱(chēng)空間 提供了高級(jí) GDI+ 圖像處理功能。 4. System.Drawing.Text名稱(chēng)空間 提供了高級(jí) GDI+ 字體和文本排版功能 共八十九頁(yè)14.1.1 Graphics類(lèi) Graphics類(lèi)包含在System.Drawing名稱(chēng)(mngchng)空間下。要進(jìn)行圖形處理,必須首先創(chuàng)建Graphics對(duì)象,然后才能利用它進(jìn)行各種畫(huà)圖操作。 創(chuàng)建Graphics對(duì)象的形式有: 1.在窗體或控件的Paint事件中直接引用Graphics對(duì)象 每一個(gè)窗體或控件都有一個(gè)Paint事件,該事件的參數(shù)中包含了當(dāng)前窗體或控件的Graphics對(duì)象

4、,在為窗體或控件創(chuàng)建繪制代碼時(shí),一般使用此方法來(lái)獲取對(duì)圖形對(duì)象的引用。Private void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics g=e.Graphics; 共八十九頁(yè) 2.從當(dāng)前窗體獲取對(duì)Graphics對(duì)象的引用 把當(dāng)前窗體的畫(huà)刷、字體、顏色作為缺省值獲取對(duì)Graphics對(duì)象的引用,注意這種對(duì)象只有在處理當(dāng)前Windows窗口消息(xio xi)的過(guò)程中有效。如果想在已存在的窗體或控件上繪圖,可以使用此方法。例如: Graphics g=this.CreatGraphics(

5、); 3.從繼承自圖像的任何對(duì)象創(chuàng)建Graphics對(duì)象。 此方法在需要更改已存在的圖像時(shí)十分有用。例如: Bitmap bitmap=new Bitmap(C:testa1.bmp); Graphics g=Graphics.FromImage(bitmap);共八十九頁(yè)14.1.2 顏色(yns) 顏色是進(jìn)行圖形操作的基本要素。任何一種顏色都可以由四個(gè)分量決定,每個(gè)分量占據(jù)一個(gè)字節(jié): R:紅色,取值范圍0255,255為飽和紅色。 G:綠色,取值范圍0255,255為飽和綠色。 B:藍(lán)色,取值范圍0255,255為飽和藍(lán)色。 A:Alpha值,即透明度。取值范圍0255,0為完全(wnqu

6、n)透明,255為完全不透明。 在System.Drawing名稱(chēng)空間下,有一個(gè)Color結(jié)構(gòu)類(lèi)型,可以使用下列方法創(chuàng)建顏色對(duì)象: 使用FromArgb指定任意顏色 這個(gè)方法有兩種常用的形式,第一種形式是直接指定三種顏色,方法原型為:共八十九頁(yè) public static Color FromArgb(int red,int green,int blue)三個(gè)參數(shù)(cnsh)分別表示R、G、B三色,Alpha值使用缺省值255,即完全不透明。例如:Color red=Color.FromArgb(255,0,0);Color green=Color.FromArgb(0,255,0);Colo

7、r blue=Color.FromArgb(0,0,0 xff); 其中,0 xff為十六進(jìn)制表示形式。 第二種形式使用四個(gè)參數(shù),格式為:public static Color FromArgb(int alpha,int red,int green,int blue)四個(gè)參數(shù)分別表示透明度和R、G、B三色值。共八十九頁(yè) 使用系統(tǒng)預(yù)定義顏色 在Color結(jié)構(gòu)中已經(jīng)(y jing)預(yù)定義了141種顏色,可以直接使用,例如: Color myColor; myColor = Color.Red; myColor = Color.Aquamarine; myColor = Color.LightGo

8、ldenrodYellow; 共八十九頁(yè)14.1.3 筆和畫(huà)筆(hu b) 在GDI+中,可使用筆對(duì)象和畫(huà)筆對(duì)象呈現(xiàn)圖形、文本和圖像(t xin)。筆是Pen類(lèi)的實(shí)例,用于繪制線(xiàn)條和空心形狀。畫(huà)筆是從Brush類(lèi)派生的任何類(lèi)的實(shí)例,用于填充形狀或繪制文本。 1. 筆(Pen) 筆可用于繪制繪制具有指定寬度和樣式的線(xiàn)條、曲線(xiàn)以及勾勒形狀輪廓。 下面的示例說(shuō)明如何創(chuàng)建一支基本的黑色筆: Pen myPen = new Pen(Color.Black); Pen myPen = new Pen(Color.Black, 5); 也可以從畫(huà)筆對(duì)象創(chuàng)建筆,例如: SolidBrush myBrush =

9、 new SolidBrush(Color.Red); Pen myPen = new Pen(myBrush); Pen myPen = new Pen(myBrush, 5); 共八十九頁(yè)筆(Pen)的用法演示示例。 1) 新建一個(gè)Windows應(yīng)用程序,適當(dāng)(shdng)加寬窗體寬度。然后切換到代碼方式,添加名稱(chēng)空間引用: using System.Drawing.Drawing2D; 2) 添加Form1_Paint事件代碼。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Gr

10、aphics g = e.Graphics;Pen pen = new Pen(Color.Blue,10.5f); g.DrawString(藍(lán)色,寬度為10.5,this.Font, new SolidBrush(Color.Black),5,5);g.DrawLine(pen,new Point(110,10),new Point(380,10);pen.Width=2; pen.Color=Color.Red;g.DrawString(紅色,寬度為2,this.Font, new SolidBrush(Color.Black),5,25);共八十九頁(yè)g.DrawLine(pen,new

11、 Point(110,30),new Point(380,30);pen.StartCap=LineCap.Flat;pen.EndCap=LineCap.ArrowAnchor;pen.Width=9;g.DrawString(紅色(hngs)箭頭線(xiàn),this.Font, new SolidBrush(Color.Black),5,45);g.DrawLine(pen,new Point(110,50),new Point(380,50);pen.DashStyle = DashStyle.Custom;pen.DashPattern=new float4,4;pen.Width=2; pe

12、n.EndCap=LineCap.NoAnchor;g.DrawString(自定義虛線(xiàn),this.Font, new SolidBrush(Color.Black),5,65);g.DrawLine(pen,new Point(110,40),new Point(380,70);pen.DashStyle=DashStyle.Dot;g.DrawString(點(diǎn)劃線(xiàn),this.Font, new SolidBrush(Color.Black),5,85);g.DrawLine(pen,new Point(110,90),new Point(380,90); 共八十九頁(yè)運(yùn)行(ynxng)結(jié)果

13、共八十九頁(yè)2、畫(huà)刷(Brush) 畫(huà)刷是可與Graphics對(duì)象一起使用來(lái)創(chuàng)建實(shí)心形狀和呈現(xiàn)(chngxin)文本的對(duì)象??梢杂卯?huà)筆填充各種圖形形狀,如矩形、橢圓、扇形、多邊形和封閉路徑等。 幾種不同類(lèi)型的畫(huà)刷: SolidBrush 畫(huà)刷最簡(jiǎn)單的形式,用純色進(jìn)行繪制。 HatchBrush 類(lèi)似于 SolidBrush,但是可以利用該類(lèi)從大量預(yù)設(shè)的圖案中選擇繪制時(shí)要使用的圖案,而不是純色。 TextureBrush 使用紋理(如圖像)進(jìn)行繪制。 LinearGradientBrush 使用沿漸變混合的兩種顏色進(jìn)行繪制。 PathGradientBrush 基于編程者定義的唯一路徑,使用復(fù)雜的

14、混合色漸變進(jìn)行繪制。共八十九頁(yè)(1)使用SolidBrush類(lèi)定義單色畫(huà)筆 SolidBrush類(lèi)用于定義單色畫(huà)筆。該類(lèi)只有一個(gè)構(gòu)造函數(shù),帶有一個(gè)Color類(lèi)型(lixng)的參數(shù)。 下面的示例說(shuō)明如何在窗體上繪制一個(gè)純紅色的橢圓。該橢圓將符合為其提供的矩形的大?。ù死袨楸硎菊麄€(gè)窗體的ClientRectangle)。【例】單色畫(huà)刷演示示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g =e.Graphics;SolidBrush myBrush = new

15、SolidBrush(Color.Red);g.FillEllipse(myBrush, this.ClientRectangle); 共八十九頁(yè)運(yùn)行(ynxng)效果共八十九頁(yè)(2)使用HatchBrush類(lèi)繪制簡(jiǎn)單圖案 HatchBrush類(lèi)用于從大量預(yù)設(shè)的圖案中選擇繪制時(shí)要使用的圖案,而不是純色。 下面(xi mian)的示例說(shuō)明如何創(chuàng)建一個(gè)HatchBrush,它使用90%的陰影,前景色與背景色的比例為90:100,并使用白色作為前景色,黑色作為背景色?!纠刻畛浜?jiǎn)單圖案示例。 private void Form1_Paint(object sender, System.Windows

16、.Forms.PaintEventArgs e)Graphics g = e.Graphics;System.Drawing.Drawing2D.HatchBrush aHatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent90, Color.White, Color.Black);g.FillEllipse(aHatchBrush, this.ClientRectangle); 共八十九頁(yè)運(yùn)行(ynxng)效果:共八十九頁(yè)(3)使用TextureBrush

17、類(lèi)繪制(huzh)復(fù)雜圖案 TextureBrush類(lèi)允許使用一幅圖像作為填充的樣式。該類(lèi)提供了5個(gè)重載的構(gòu)造函數(shù),分別是: Public TextureBrush(Image) Public TextureBrush(Image,Rectangle) Public TextureBrush(Image,WrapMode) Public TextureBrush(Image,Rectangle,ImageAttributes) Public TextureBrush(Image,WrapMode,Rectangle) 其中:Image:Image對(duì)象用于指定畫(huà)筆的填充圖案。 Rectangle

18、:Rectangle對(duì)象用于指定圖像上用于畫(huà)筆的矩形區(qū) 域,其位置不能超越圖像的范圍。 WrapMode:WrapMode枚舉成員用于指定如何排布圖像,可以是 Clamp 完全由繪制對(duì)象的邊框決定 Tile 平鋪 TileFlipX 水平方向翻轉(zhuǎn)并平鋪圖像 TileFlipY 垂直方向翻轉(zhuǎn)并平鋪圖像 TileFlipXY 水平和垂直方向翻轉(zhuǎn)并平鋪圖像共八十九頁(yè) ImageAttributes:ImageAttributes對(duì)象用于指定圖像的附加特 性參數(shù)。 TextureBrush類(lèi)有三個(gè)屬性(shxng): Image:Image類(lèi)型,與畫(huà)筆關(guān)聯(lián)的圖像對(duì)象。 Transform:Matrix

19、類(lèi)型,畫(huà)筆的變換矩陣。 WrapMode:WrapMode枚舉成員,指定圖像的排布方式。 下面的示例說(shuō)明了如何創(chuàng)建一個(gè)TextureBrush,例子使用名為m23.jpg的圖像進(jìn)行繪制。 【例】創(chuàng)建TextureBrush示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g = e.Graphics;TextureBrush myBrush = new TextureBrush(new Bitmap(e:testm23.jpg);g.FillEllipse(myB

20、rush, this.ClientRectangle); 共八十九頁(yè)運(yùn)行(ynxng)效果:共八十九頁(yè)(4)使用LinearGradientBrush類(lèi)定義線(xiàn)性漸變 這個(gè)(zh ge)類(lèi)用于定義線(xiàn)性漸變畫(huà)筆,可以是雙色漸變,也可以是多色漸變。缺省情況下,漸變由起始顏色沿著水平方向平均過(guò)渡到終止顏色。要定義多色漸變,需要使用InterpolationColors屬性。下面的示例說(shuō)明如何由白色漸變到藍(lán)色?!纠烤€(xiàn)性漸變示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics

21、g = e.Graphics;System.Drawing.Drawing2D.LinearGradientBrush myBrush = new System.Drawing.Drawing2D.LinearGradientBrush( this.ClientRectangle,Color.White, Color.Blue, System.Drawing.Drawing2D.LinearGradientMode.Vertical); g.FillRectangle(myBrush, this.ClientRectangle);共八十九頁(yè) 如果創(chuàng)建應(yīng)用程序后向設(shè)計(jì)窗體上拖放一些控件,可以看到

22、運(yùn)行后該圖就是(jish)一個(gè)漂亮的背景了。 共八十九頁(yè)(5)使用PathGradientBrush類(lèi)實(shí)現(xiàn)彩色漸變 在GDI+中,把一個(gè)或多個(gè)圖形組成的形體(xngt)稱(chēng)作路徑??梢允褂肎raphicsPath類(lèi)定義路徑,使用PathGradientBrush類(lèi)定義路徑內(nèi)部的漸變色畫(huà)筆。漸變色從路徑內(nèi)部的中心點(diǎn)逐漸過(guò)渡到路徑的外邊界邊緣。 PathGradientBrush類(lèi)有三種形式的構(gòu)造函數(shù),形式之一是: public PathGradientBrush(GraphicsPath path) 其中,GraphicsPath定義畫(huà)筆填充的區(qū)域。【例】路徑和路徑畫(huà)筆的使用。 using Sys

23、tem.Drawing.Drawing2D; 共八十九頁(yè)private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g=e.Graphics;Point centerPoint=new Point(150,100);int R=60; GraphicsPath path=new GraphicsPath();path.AddEllipse(centerPoint.X-R,centerPoint.Y-R,2*R,2*R);PathGradientBrush brush=new Pat

24、hGradientBrush(path); /指定路徑(ljng)中心點(diǎn)brush.CenterPoint=centerPoint;/指定路徑中心點(diǎn)的顏色brush.CenterColor=Color.Red;/Color類(lèi)型的數(shù)組指定與路徑上每個(gè)頂點(diǎn)對(duì)應(yīng)的顏色brush.SurroundColors=new Color Color.Plum ;共八十九頁(yè)g.FillEllipse(brush,centerPoint.X-R,centerPoint.Y-R, 2*R,2*R); centerPoint=new Point(350,100); R=20;path=new GraphicsPath

25、();path.AddEllipse(centerPoint.X-R,centerPoint.Y-R,2*R,2*R);path.AddEllipse(centerPoint.X-2*R,centerPoint.Y-2*R, 4*R,4*R);path.AddEllipse(centerPoint.X-3*R,centerPoint.Y-3*R, 6*R,6*R); brush=new PathGradientBrush(path);brush.CenterPoint=centerPoint;brush.CenterColor=Color.Red;brush.SurroundColors=ne

26、w Color Color.Black,Color.Blue,Color.Green ;g.FillPath(brush,path);共八十九頁(yè) 在這個(gè)例子中,可以看到當(dāng)使用FillPath()方法填充路徑的時(shí)候,如果多個(gè)圖形互相重疊(chngdi),則重疊(chngdi)部分的數(shù)目為偶數(shù)時(shí)不會(huì)被填充,因此右圖中間部分仍為背景色而不是藍(lán)色。 共八十九頁(yè)14.1.4 平移(pn y)、旋轉(zhuǎn)與縮放 Graphics類(lèi)提供了三種對(duì)圖像進(jìn)行幾何變換的方法,它們是TranslateTransform()方法、RotateTransform()方法和ScaleTransform()方法,分別用于圖形圖像的

27、平移、旋轉(zhuǎn)和縮放。 TranslateTransform()方法的形式為: public void TranslateTransform(float dx,float dy) 其中,dx表示平移的x分量,dy表示平移的y分量。 RotateTransform()方法的形式為: public void RotateTransform(float angle) 其中,angle表示旋轉(zhuǎn)角度(jiod)。 ScaleTransform()方法的形式為: public void ScaleTransform(float sx,float sy) 其中,sx表示x方向的縮放比例,sy表示y方向的縮放比例

28、。 共八十九頁(yè)【例】三種變換方法示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g = e.Graphics;/橢圓透明度80%g.FillEllipse(new SolidBrush(Color.FromArgb(80,Color.Red), 120,30,200,100);g.RotateTransform(30.0f); /順時(shí)針旋轉(zhuǎn)10度g.FillEllipse(new SolidBrush(Color.FromArgb(80,Color.Blue),

29、 120,30,200,100);/水平方向向右平移200個(gè)像素,垂直方向(fngxing)向上平移100個(gè)像素g.TranslateTransform(200.0f,-100.0f);g.FillEllipse(new SolidBrush(Color.FromArgb(50,Color.Green), 120,30,200,100);g.ScaleTransform(0.5f,0.5f); /縮小到一半 g.FillEllipse(newSolidBrush(Color.FromArgb(100,Color.Red), 120,30,200,100); 共八十九頁(yè)共八十九頁(yè)14.2 繪制(

30、huzh)圖形 所有繪制圖形的方法都位于Graphics中。14.2.1 直線(xiàn) 有兩種繪制直線(xiàn)的方法:DrawLine()方法和DrawLines()方法。 DrawLine()用于繪制一條直線(xiàn),DrawLines()用于繪制多條直線(xiàn)。常用形式有: public void DrawLine(Pen pen,Point pt1,Point pt2) 其中Pen對(duì)象確定線(xiàn)條的顏色、寬度(kund)和樣式。Point結(jié)構(gòu)確定起點(diǎn)和終點(diǎn)。例如:private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Gr

31、aphics g = e.Graphics;Pen blackPen = new Pen(Color.Black, 3);Point point1 = new Point(100, 100);Point point2 = new Point(200, 100);e.Graphics.DrawLine(blackPen, point1, point2); 共八十九頁(yè) public void DrawLine(Pen pen,int x1,int y1,int x2,int y2) 其中x1,y1為起點(diǎn)坐標(biāo),x2,y2為終點(diǎn)坐標(biāo)。例如:e.Graphics.DrawLine(blackPen, 1

32、00,100,200,100); public void DrawLines(Pen pen,Point points) 這種方法(fngf)用于繪制一系列連接一組終結(jié)點(diǎn)的線(xiàn)條。數(shù)組中的前兩個(gè)點(diǎn)指定第一條線(xiàn)。每個(gè)附加點(diǎn)指定一個(gè)線(xiàn)段的終結(jié)點(diǎn),該線(xiàn)段的起始點(diǎn)是前一條線(xiàn)段的結(jié)束點(diǎn)。private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g = e.Graphics;Pen pen = new Pen(Color.Black, 3);Point points =new Point( 1

33、0, 10),new Point( 10, 100),new Point(200, 50),new Point(250, 120); e.Graphics.DrawLines(pen, points); 共八十九頁(yè)效果(xiogu) 共八十九頁(yè)14.2.2 矩形 由于(yuy)矩形具有輪廓和封閉區(qū)域,所以C#提供了兩類(lèi)繪制矩形的方法,一類(lèi)用于繪制矩形的輪廓,另一類(lèi)用于填充矩形的封閉區(qū)域。 使用DrawRectangle()或DrawRectangles()方法繪制矩形輪廓的常用形式有:public void DrawRectangle(Pen pen, Rectangle rect) 其中re

34、ct表示要繪制的矩形的Rectangle結(jié)構(gòu)?!纠坷L制矩形輪廓示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g = e.Graphics;Pen pen = new Pen(Color.Black, 3);Rectangle rect = new Rectangle( 30, 30, 200, 100);e.Graphics.DrawRectangle(pen, rect); 共八十九頁(yè) public void DrawRectangle(Pen pen,

35、int x, int y, int width, int height) 其中x, y為矩形左上角坐標(biāo)值。例如: e.Graphics.DrawRectangle(pen, 20,20,200,100); public void DrawRectangles(Pen pen, Rectangle rects) 該方法(fngf)用于繪制多個(gè)矩形?!纠渴褂肈rawRectangles方法繪制矩形輪廓示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g = e.Gr

36、aphics;Pen pen = new Pen(Color.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); 共八十九頁(yè)共八十九頁(yè)14.2.3 多邊形 由于多邊形也是封閉的,所以C#中也有兩種繪制方法:使用(shyng)DrawPolygon()方法繪制多邊形輪廓,使用FillPolygon()方法填充多邊形的封閉區(qū)域。 下

37、面的例子說(shuō)明了這些方法的使用形式?!纠坷L制多邊形示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Graphics g = e.Graphics;Pen pen = new Pen(Color.Red);Point points = new Point(50,50),new Point(100,100), new Point(45,150), new Point(25,150),new Point(0,100);e.Graphics.DrawPolygon(pen, points

38、);points =new Point new Point(250,50),new Point(300,100),new Point(275,150), new Point(225,150),new Point(200,100);g.FillPolygon(new SolidBrush(Color.Red),points); 共八十九頁(yè)共八十九頁(yè)14.2.4 曲線(xiàn) 這里所講的曲線(xiàn)是指自定義曲線(xiàn),自定義曲線(xiàn)有兩種形式:打開(kāi)的曲線(xiàn)和封閉的曲線(xiàn) 。 在Graphics類(lèi)中,繪制自定義曲線(xiàn)的方法有: DrawCurve()方法 DrawClosedCurve()方法 以及(yj)應(yīng)用廣泛的繪制貝塞爾曲

39、線(xiàn)的 DrawBezier()方法 DrawBeziers()方法。 共八十九頁(yè)1、DrawCurve()方法 這個(gè)方法用光滑的曲線(xiàn)把給定的點(diǎn)連接起來(lái),常用形式有: public void DrawCurve(Pen pen, Point points) 其中,Point結(jié)構(gòu)類(lèi)型的數(shù)組中指明各節(jié)點(diǎn),默認(rèn)彎曲強(qiáng)度為0.5,注意數(shù)組中至少要有4個(gè)元素。 public void DrawCurve(Pen pen, Point points, float tension) 其中tension指定彎曲強(qiáng)度,該值范圍為0.0f 1.0f,超出此范圍會(huì)產(chǎn)生異常,當(dāng)彎曲強(qiáng)度為零時(shí),就是(jish)直線(xiàn)。 共

40、八十九頁(yè)【例】繪制(huzh)直線(xiàn)與平滑曲線(xiàn)。private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)Pen redPen = new Pen(Color.Red, 3);Pen greenPen = new Pen(Color.Green, 3);Point curvePoints =new Point( 50, 250),new Point(100, 25),new Point(200, 250),new Point(250, 50),new Point(300, 75),new Point

41、(350, 200),new Point(400, 150);e.Graphics.DrawLines(redPen, curvePoints);e.Graphics.DrawCurve(greenPen, curvePoints); 共八十九頁(yè)共八十九頁(yè)2、DrawClosedCurve()方法 這個(gè)方法也是用平滑的曲線(xiàn)將各節(jié)點(diǎn)連接起來(lái),但會(huì)自動(dòng)把首尾節(jié)點(diǎn)連接起來(lái)構(gòu)成封閉曲線(xiàn)。3、貝塞爾曲線(xiàn) 每段貝塞爾曲線(xiàn)都需要四個(gè)點(diǎn),第一個(gè)點(diǎn)是起始點(diǎn),第四個(gè)點(diǎn)是終止(zhngzh)點(diǎn),第二個(gè)點(diǎn)和第三個(gè)點(diǎn)控制曲線(xiàn)的形狀。使用DrawBezier()方法繪制一段貝塞爾曲線(xiàn),使用DrawBeziers()方法繪

42、制多段貝塞爾曲線(xiàn)。常用形式有: public void DrawBezier(Pen pen, Point pt1, Point pt2, Point pt3, Point pt4) 其中pt1、pt2、pt3、pt4分別指定四個(gè)點(diǎn)。 public void DrawBezier(Pen pen, Point points) 其中points是Point結(jié)構(gòu)的數(shù)組,第一段貝塞爾曲線(xiàn)從點(diǎn)數(shù)組中的第一個(gè)點(diǎn)到第四個(gè)點(diǎn)繪制而成。以后每段曲線(xiàn)只需要三個(gè)點(diǎn):兩個(gè)控制點(diǎn)和一個(gè)結(jié)束點(diǎn)。前一段曲線(xiàn)的結(jié)束點(diǎn)會(huì)自動(dòng)用作后一段曲線(xiàn)的起始點(diǎn)。 共八十九頁(yè)【例】繪制(huzh)貝塞爾曲線(xiàn)。private void For

43、m1_Paint(object sender, System.Windows.Forms.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) ;e.Graphics.DrawBeziers(blackPen, bezierPoint

44、s); 共八十九頁(yè)共八十九頁(yè)14.2.5 橢圓(tuyun) 橢圓是一種特殊的封閉曲線(xiàn)(qxin),Graphics類(lèi)專(zhuān)門(mén)提供了繪制橢圓的兩種方法:DrawEllipse()方法和FillEllipse()方法。常用形式有: public void DrawEllipse(Pen pen, Rectangle rect) 其中rect為Rectangle結(jié)構(gòu),用于確定橢圓的邊界。 public void DrawEllipse(Pen pen, int x, int y, int width, int height) 其中x, y為橢圓左上角的坐標(biāo),width定義橢圓的邊框的寬度,height

45、定義橢圓的邊框的高度。 public void FillEllipse(Pen pen, Rectangle rect) 填充橢圓的內(nèi)部區(qū)域。其中rect為Rectangle結(jié)構(gòu),用于確定橢圓的邊界。 public void FillEllipse(Pen pen, int x, int y, int width, int height) 填充橢圓的內(nèi)部區(qū)域。其中x, y為橢圓左上角的坐標(biāo),width定義橢圓的邊框的寬度,height定義橢圓的邊框的高度。 共八十九頁(yè)14.3圖像(t xin)的顯示與保存 14.3.1 顯示圖像 可以使用 GDI+ 顯示以文件形式存在的圖像。圖像文件可以是BM

46、P、JPEG、GIF、TIFF、PNG等。實(shí)現(xiàn)步驟為: 創(chuàng)建一個(gè)Bitmap對(duì)象,指明要顯示的圖像文件; 創(chuàng)建一個(gè)Graphics對(duì)象,表示要使用的繪圖平面; 調(diào)用 Graphics 對(duì)象的 DrawImage 方法顯示圖像。 創(chuàng)建Bitmap對(duì)象 Bitmap類(lèi)有很多重載的構(gòu)造函數(shù),其中(qzhng)之一是: Public Bitmap(string filename) 可以利用該構(gòu)造函數(shù)創(chuàng)建Bitmap對(duì)象,例如: Bitmap bitmap = new Bitmap(“tu1.jpg”); 共八十九頁(yè) DrawImage()方法 Graphics類(lèi)的DrawImage()方法用于在指定位

47、置顯示原始圖像或者縮放后的圖像。該方法的重載(zhn zi)形式非常多,其中之一為: public void DrawImage(Image image, int x, int y, int width, int height) 該方法在x, y按指定的大小顯示圖像。利用這個(gè)方法可以直接顯示縮放后的圖像。 【例】假設(shè)窗體中有一個(gè)Button按鈕button1,可以在單擊按鈕的事件代碼中顯示圖像。private void button1_Click(object sender, System.EventArgs e)Bitmap bitmap = new Bitmap(d:testtu1.jpg

48、);Graphics g = this.CreateGraphics();g.DrawImage(bitmap,3,10,200,200);g.DrawImage(bitmap,250,10,50,50);g.DrawImage(bitmap,350,10,bitmap.Width/2, bitmap.Height/2);共八十九頁(yè)共八十九頁(yè)14.3.2 保存圖像 使用畫(huà)圖功能在窗體上繪制出圖形或者(huzh)圖像后,可以以多種格式保存到圖像文件中. 下面的例子說(shuō)明了具體的用法?!纠繉⒗L制的圖形或圖像保存到文件中。 創(chuàng)建一個(gè)Windows應(yīng)用程序,設(shè)計(jì)畫(huà)面: 共八十九頁(yè) 添加名稱(chēng)空間引用us

49、ing System.Drawing.Drawing2D; 添加【畫(huà)圖(hu t)】按鈕的Click事件代碼private void button1_Click(object sender, System.EventArgs e)Graphics g = this.CreateGraphics();DrawMyImage(g); 添加調(diào)用的方法private void DrawMyImage(Graphics g)Rectangle rect1=new Rectangle(0,0,this.Width/4, this.Height-100);HatchBrush hatchBrush = ne

50、w HatchBrush(HatchStyle.Shingle, Color.White, Color.Black);g.FillEllipse(hatchBrush, rect1);Rectangle rect2=new Rectangle(this.Width/4+50,0, this.Width/4,this.Height-100);共八十九頁(yè)hatchBrush = new HatchBrush(HatchStyle.WideUpwardDiagonal, Color.White, Color.Red);g.FillRectangle(hatchBrush,rect2);int x=t

51、his.Width-50-this.Width/4;Point points =new Point new Point(x,10),new Point(x+50,60),new Point(x+150,10),new Point(x+200,160),new Point(x+150,260),new Point(x+50,260),new Point(x,160);hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.White, Color.Red);TextureBrush myBrush = new TextureBrus

52、h(new Bitmap(e:testm23.jpg);g.FillClosedCurve(myBrush,points);共八十九頁(yè) 添加【保存】按鈕的Click事件代碼private void button2_Click(object sender, System.EventArgs e)/構(gòu)造一個(gè)指定區(qū)域(qy)的空?qǐng)D像Bitmap image=new Bitmap(this.Width,this.Height-100);/根據(jù)指定區(qū)域得到Graphics對(duì)象Graphics g=Graphics.FromImage(image);/設(shè)置圖像的背景色g.Clear(this.BackCo

53、lor);/將圖形畫(huà)到Graphics對(duì)象中DrawMyImage(g);try/保存畫(huà)到Graphics對(duì)象中的圖形image.Save(d:testtu1.jpg,System.Drawing.Imaging.ImageFormat.Jpeg);共八十九頁(yè)g=this.CreateGraphics();Rectangle rect=new Rectangle(0,0,this.Width,this.Height-100);g.FillRectangle(new SolidBrush(this.BackColor),rect);MessageBox.Show(保存成功(chnggng)!,恭

54、喜);catch(Exception err)MessageBox.Show(err.Message); 添加【顯示】按鈕的Click事件代碼private void button3_Click(object sender, System.EventArgs e)Rectangle rect=new Rectangle(0,0,this.Width,this.Height-100);Graphics g=this.CreateGraphics();Image image=new Bitmap(d:testtu1.jpg);g.DrawImage(image,rect);共八十九頁(yè)共八十九頁(yè)14

55、.4 動(dòng)畫(huà)設(shè)計(jì) 14.4.1奔跑的豹子 分別設(shè)計(jì)豹子奔跑的八種不同形狀,得到八個(gè)圖像,保存到文件名為t1.jpg到t8.jpg中。設(shè)計(jì)3只豹子從左向右奔跑的動(dòng)畫(huà),要求能隨時(shí)調(diào)整奔跑的速度。 1)創(chuàng)建一個(gè)(y )Windows應(yīng)用程序,設(shè)置窗體的背景色為白色;向設(shè)計(jì)窗體拖放三個(gè)【PicturePox】控件,【SizeMode】屬性均為【StretchImage】,調(diào)整三只豹子的大小使其看起來(lái)有立體感;一個(gè)TrackBar控件,兩個(gè)Label控件,1個(gè)Timer控件。如圖: 共八十九頁(yè)共八十九頁(yè)源程序如下(rxi):using System.Drawing.Drawing2D;namespace

56、Donghuapublic class Form1 : System.Windows.Forms.FormBitmap bitmap;int num;private System.Windows.Forms.TrackBar trackBar1;private System.Windows.Forms.Timer timer1;private System.Windows.Forms.Label label1;private System.Windows.Forms.Label label2;private System.Windows.Forms.PictureBox pictureBox1

57、;private System.Windows.Forms.PictureBox pictureBox2;private System.Windows.Forms.PictureBox pictureBox3;private System.ComponentModel.IContainer components;public Form1()InitializeComponent();共八十九頁(yè)this.trackBar1.Minimum=10;this.trackBar1.Maximum=100;this.trackBar1.Value=50;this.timer1.Interval=this

58、.trackBar1.Value;bitmap=new Bitmap8;for(int i=1;i=8;i+)bitmapi-1=new Bitmap(d:testt+i.ToString()+.jpg);num=0;this.timer1.Enabled=true; private void trackBar1_Scroll(object sender, System.EventArgs e)this.timer1.Interval=this.trackBar1.Value;共八十九頁(yè)private void timer1_Tick(object sender, System.EventAr

59、gs e)if(num700)this.pictureBox1.Left=-100;this.pictureBox2.Left=this.pictureBox1.Left-20;this.pictureBox3.Left=this.pictureBox2.Left-20;共八十九頁(yè)elsethis.pictureBox1.Left+=10;this.pictureBox2.Left=this.pictureBox1.Left-20;this.pictureBox3.Left=this.pictureBox2.Left-20;3)運(yùn)行,觀(guān)察(gunch)效果。 共八十九頁(yè)14.4.2 圖像(t

60、xin)變換 創(chuàng)建一個(gè)Windows應(yīng)用程序,向窗體拖放1個(gè)PictureBox控件,黑色背景;1個(gè)ListBox控件,5號(hào)字;2個(gè)Button控件。設(shè)計(jì)(shj)畫(huà)面: 共八十九頁(yè) 源程序如下:using System.Drawing.Drawing2D;namespace WinTestpublic class Form1 : System.Windows.Forms.FormBitmap myBitmap;public Form1()InitializeComponent();string items=左到右拉伸(l shn),上到下拉伸,中間到四周擴(kuò)散,反轉(zhuǎn),中間向兩邊拉伸,共八十九頁(yè)

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論