第14章++圖形圖像與多媒體編程.ppt_第1頁
第14章++圖形圖像與多媒體編程.ppt_第2頁
第14章++圖形圖像與多媒體編程.ppt_第3頁
第14章++圖形圖像與多媒體編程.ppt_第4頁
第14章++圖形圖像與多媒體編程.ppt_第5頁
已閱讀5頁,還剩83頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

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

2、 GDI+ 為使用各種字體、字號和樣式來顯示文本這種復(fù)雜任務(wù)提供了大量的支持。 其他高級功能,在C#中,所有圖形圖像處理功能都包含在以下名稱空間下: 1. System.Drawing名稱空間 提供了對GDI+基本圖形功能的訪問,主要有Graphics類、Bitmap類、從Brush類繼承的類、Font類、Icon類、Image類、Pen類、Color類等。 2. System.Drawing.Drawing2D名稱空間 提供了高級的二維和矢量圖形功能。主要有梯度型畫刷、Matrix類(用于定義幾何變換)和GraphicsPath類等。 3. System.Drawing.Imaging名稱空

3、間 提供了高級 GDI+ 圖像處理功能。 4. System.Drawing.Text名稱空間 提供了高級 GDI+ 字體和文本排版功能,14.1.1 Graphics類,Graphics類包含在System.Drawing名稱空間下。要進(jìn)行圖形處理,必須首先創(chuàng)建Graphics對象,然后才能利用它進(jìn)行各種畫圖操作。 創(chuàng)建Graphics對象的形式有: 1.在窗體或控件的Paint事件中直接引用Graphics對象 每一個窗體或控件都有一個Paint事件,該事件的參數(shù)中包含了當(dāng)前窗體或控件的Graphics對象,在為窗體或控件創(chuàng)建繪制代碼時,一般使用此方法來獲取對圖形對象的引用。 Privat

4、e void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics g=e.Graphics; ,2.從當(dāng)前窗體獲取對Graphics對象的引用 把當(dāng)前窗體的畫刷、字體、顏色作為缺省值獲取對Graphics對象的引用,注意這種對象只有在處理當(dāng)前Windows窗口消息的過程中有效。如果想在已存在的窗體或控件上繪圖,可以使用此方法。例如: Graphics g=this.CreatGraphics(); 3.從繼承自圖像的任何對象創(chuàng)建Graphics對象。 此方法在需要更改已存在的圖像時十分有用。例如:

5、Bitmap bitmap=new Bitmap(C:testa1.bmp); Graphics g=Graphics.FromImage(bitmap);,14.1.2 顏色,顏色是進(jìn)行圖形操作的基本要素。任何一種顏色都可以由四個分量決定,每個分量占據(jù)一個字節(jié): R:紅色,取值范圍0255,255為飽和紅色。 G:綠色,取值范圍0255,255為飽和綠色。 B:藍(lán)色,取值范圍0255,255為飽和藍(lán)色。 A:Alpha值,即透明度。取值范圍0255,0為完全透明,255為完全不透明。 在System.Drawing名稱空間下,有一個Color結(jié)構(gòu)類型,可以使用下列方法創(chuàng)建顏色對象: 使用Fr

6、omArgb指定任意顏色 這個方法有兩種常用的形式,第一種形式是直接指定三種顏色,方法原型為:, public static Color FromArgb(int red,int green,int blue) 三個參數(shù)分別表示R、G、B三色,Alpha值使用缺省值255,即完全不透明。例如: Color red=Color.FromArgb(255,0,0); Color green=Color.FromArgb(0,255,0); Color blue=Color.FromArgb(0,0,0 xff); 其中,0 xff為十六進(jìn)制表示形式。 第二種形式使用四個參數(shù),格式為: public

7、 static Color FromArgb(int alpha,int red,int green,int blue) 四個參數(shù)分別表示透明度和R、G、B三色值。,使用系統(tǒng)預(yù)定義顏色 在Color結(jié)構(gòu)中已經(jīng)預(yù)定義了141種顏色,可以直接使用,例如: Color myColor; myColor = Color.Red; myColor = Color.Aquamarine; myColor = Color.LightGoldenrodYellow;,14.1.3 筆和畫筆,在GDI+中,可使用筆對象和畫筆對象呈現(xiàn)圖形、文本和圖像。筆是Pen類的實例,用于繪制線條和空心形狀。畫筆是從Brush

8、類派生的任何類的實例,用于填充形狀或繪制文本。 1. 筆(Pen) 筆可用于繪制繪制具有指定寬度和樣式的線條、曲線以及勾勒形狀輪廓。 下面的示例說明如何創(chuàng)建一支基本的黑色筆: Pen myPen = new Pen(Color.Black); Pen myPen = new Pen(Color.Black, 5); 也可以從畫筆對象創(chuàng)建筆,例如: SolidBrush myBrush = new SolidBrush(Color.Red); Pen myPen = new Pen(myBrush); Pen myPen = new Pen(myBrush, 5);,筆(Pen)的用法演示示例。

9、 1) 新建一個Windows應(yīng)用程序,適當(dāng)加寬窗體寬度。然后切換到代碼方式,添加名稱空間引用: using System.Drawing.Drawing2D; 2) 添加Form1_Paint事件代碼。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics; Pen pen = new Pen(Color.Blue,10.5f); g.DrawString(藍(lán)色,寬度為10.5,this.Font, new SolidBrush(Color.

10、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);,g.DrawLine(pen,new Point(110,30),new Point(380,30); pen.StartCap=LineCap.Flat; pen.EndCap=LineCap.ArrowAnchor; pen.Width=9; g.DrawStri

11、ng(紅色箭頭線,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; pen.EndCap=LineCap.NoAnchor; g.DrawString(自定義虛線,this.Font, new SolidBrush(Color.Black),5,65); g.DrawLine(pen,new Poin

12、t(110,40),new Point(380,70);pen.DashStyle=DashStyle.Dot; g.DrawString(點劃線,this.Font, new SolidBrush(Color.Black),5,85); g.DrawLine(pen,new Point(110,90),new Point(380,90);,運行結(jié)果,2、畫刷(Brush) 畫刷是可與Graphics對象一起使用來創(chuàng)建實心形狀和呈現(xiàn)文本的對象??梢杂卯嫻P填充各種圖形形狀,如矩形、橢圓、扇形、多邊形和封閉路徑等。 幾種不同類型的畫刷: SolidBrush 畫刷最簡單的形式,用純色進(jìn)行繪制。 H

13、atchBrush 類似于 SolidBrush,但是可以利用該類從大量預(yù)設(shè)的圖案中選擇繪制時要使用的圖案,而不是純色。 TextureBrush 使用紋理(如圖像)進(jìn)行繪制。 LinearGradientBrush 使用沿漸變混合的兩種顏色進(jìn)行繪制。 PathGradientBrush 基于編程者定義的唯一路徑,使用復(fù)雜的混合色漸變進(jìn)行繪制。,(1)使用SolidBrush類定義單色畫筆 SolidBrush類用于定義單色畫筆。該類只有一個構(gòu)造函數(shù),帶有一個Color類型的參數(shù)。 下面的示例說明如何在窗體上繪制一個純紅色的橢圓。該橢圓將符合為其提供的矩形的大?。ù死袨楸硎菊麄€窗體的Clie

14、ntRectangle)。 【例】單色畫刷演示示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics g =e.Graphics; SolidBrush myBrush = new SolidBrush(Color.Red); g.FillEllipse(myBrush, this.ClientRectangle); ,運行效果,(2)使用HatchBrush類繪制簡單圖案 HatchBrush類用于從大量預(yù)設(shè)的圖案中選擇繪制時要使用的圖案,而不是純色。 下面的示例說

15、明如何創(chuàng)建一個HatchBrush,它使用90%的陰影,前景色與背景色的比例為90:100,并使用白色作為前景色,黑色作為背景色。 【例】填充簡單圖案示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics; System.Drawing.Drawing2D.HatchBrush aHatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing. Drawing2D.H

16、atchStyle.Percent90, Color.White, Color.Black); g.FillEllipse(aHatchBrush, this.ClientRectangle); ,運行效果:,(3)使用TextureBrush類繪制復(fù)雜圖案 TextureBrush類允許使用一幅圖像作為填充的樣式。該類提供了5個重載的構(gòu)造函數(shù),分別是: Public TextureBrush(Image) Public TextureBrush(Image,Rectangle) Public TextureBrush(Image,WrapMode) Public TextureBrush(I

17、mage,Rectangle,ImageAttributes) Public TextureBrush(Image,WrapMode,Rectangle) 其中:Image:Image對象用于指定畫筆的填充圖案。 Rectangle:Rectangle對象用于指定圖像上用于畫筆的矩形區(qū) 域,其位置不能超越圖像的范圍。 WrapMode:WrapMode枚舉成員用于指定如何排布圖像,可以是 Clamp 完全由繪制對象的邊框決定 Tile 平鋪 TileFlipX 水平方向翻轉(zhuǎn)并平鋪圖像 TileFlipY 垂直方向翻轉(zhuǎn)并平鋪圖像 TileFlipXY 水平和垂直方向翻轉(zhuǎn)并平鋪圖像,ImageAt

18、tributes:ImageAttributes對象用于指定圖像的附加特 性參數(shù)。 TextureBrush類有三個屬性: Image:Image類型,與畫筆關(guān)聯(lián)的圖像對象。 Transform:Matrix類型,畫筆的變換矩陣。 WrapMode:WrapMode枚舉成員,指定圖像的排布方式。 下面的示例說明了如何創(chuàng)建一個TextureBrush,例子使用名為m23.jpg的圖像進(jìn)行繪制。 【例】創(chuàng)建TextureBrush示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Gr

19、aphics g = e.Graphics; TextureBrush myBrush = new TextureBrush(new Bitmap(e:testm23.jpg); g.FillEllipse(myBrush, this.ClientRectangle); ,運行效果:,(4)使用LinearGradientBrush類定義線性漸變 這個類用于定義線性漸變畫筆,可以是雙色漸變,也可以是多色漸變。缺省情況下,漸變由起始顏色沿著水平方向平均過渡到終止顏色。要定義多色漸變,需要使用InterpolationColors屬性。下面的示例說明如何由白色漸變到藍(lán)色。 【例】線性漸變示例。 p

20、rivate void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics; System.Drawing.Drawing2D.LinearGradientBrush myBrush = new System.Drawing.Drawing2D.LinearGradientBrush( this.ClientRectangle,Color.White, Color.Blue, System.Drawing.Drawing2D.LinearGradientMode.Ve

21、rtical); g.FillRectangle(myBrush, this.ClientRectangle); ,如果創(chuàng)建應(yīng)用程序后向設(shè)計窗體上拖放一些控件,可以看到運行后該圖就是一個漂亮的背景了。,(5)使用PathGradientBrush類實現(xiàn)彩色漸變 在GDI+中,把一個或多個圖形組成的形體稱作路徑??梢允褂肎raphicsPath類定義路徑,使用PathGradientBrush類定義路徑內(nèi)部的漸變色畫筆。漸變色從路徑內(nèi)部的中心點逐漸過渡到路徑的外邊界邊緣。 PathGradientBrush類有三種形式的構(gòu)造函數(shù),形式之一是: public PathGradientBrush(G

22、raphicsPath path) 其中,GraphicsPath定義畫筆填充的區(qū)域。 【例】路徑和路徑畫筆的使用。 using System.Drawing.Drawing2D; ,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(cent

23、erPoint.X-R,centerPoint.Y-R,2*R,2*R); PathGradientBrush brush=new PathGradientBrush(path); /指定路徑中心點 brush.CenterPoint=centerPoint; /指定路徑中心點的顏色 brush.CenterColor=Color.Red; /Color類型的數(shù)組指定與路徑上每個頂點對應(yīng)的顏色 brush.SurroundColors=new Color Color.Plum ;,g.FillEllipse(brush,centerPoint.X-R,centerPoint.Y-R, 2*R,

24、2*R); centerPoint=new Point(350,100); R=20; path=new GraphicsPath(); 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.Center

25、Point=centerPoint; brush.CenterColor=Color.Red; brush.SurroundColors=new Color Color.Black,Color.Blue,Color.Green ; g.FillPath(brush,path); ,在這個例子中,可以看到當(dāng)使用FillPath()方法填充路徑的時候,如果多個圖形互相重疊,則重疊部分的數(shù)目為偶數(shù)時不會被填充,因此右圖中間部分仍為背景色而不是藍(lán)色。,14.1.4 平移、旋轉(zhuǎn)與縮放,Graphics類提供了三種對圖像進(jìn)行幾何變換的方法,它們是TranslateTransform()方法、RotateT

26、ransform()方法和ScaleTransform()方法,分別用于圖形圖像的平移、旋轉(zhuǎn)和縮放。 TranslateTransform()方法的形式為: public void TranslateTransform(float dx,float dy) 其中,dx表示平移的x分量,dy表示平移的y分量。 RotateTransform()方法的形式為: public void RotateTransform(float angle) 其中,angle表示旋轉(zhuǎn)角度。 ScaleTransform()方法的形式為: public void ScaleTransform(float sx,flo

27、at sy) 其中,sx表示x方向的縮放比例,sy表示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); /順時針旋轉(zhuǎn)10度 g.FillEllipse(new SolidB

28、rush(Color.FromArgb(80,Color.Blue), 120,30,200,100); /水平方向向右平移200個像素,垂直方向向上平移100個像素 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,3

29、0,200,100); ,14.2 繪制圖形,所有繪制圖形的方法都位于Graphics中。 14.2.1 直線 有兩種繪制直線的方法:DrawLine()方法和DrawLines()方法。 DrawLine()用于繪制一條直線,DrawLines()用于繪制多條直線。常用形式有: public void DrawLine(Pen pen,Point pt1,Point pt2) 其中Pen對象確定線條的顏色、寬度和樣式。Point結(jié)構(gòu)確定起點和終點。例如: private void Form1_Paint(object sender, System.Windows.Forms.PaintEve

30、ntArgs e) Graphics 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); , public void DrawLine(Pen pen,int x1,int y1,int x2,int y2) 其中x1,y1為起點坐標(biāo),x2,y2為終點坐標(biāo)。例如: e.Graphics.DrawLin

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

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

33、要繪制的矩形的Rectangle結(jié)構(gòu)。 【例】繪制矩形輪廓示例。 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);, public void DrawRectangle(Pen pen, int

34、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) 該方法用于繪制多個矩形。 【例】使用DrawRectangles方法繪制矩形輪廓示例。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics g = e.Graphics;

35、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); ,14.2.3 多邊形 由于多邊形也是封閉的,所以C#中也有兩種繪制方法:使用DrawPolygon()方法繪制多邊形輪廓,使用FillPolygon()方法填充多邊形的封閉區(qū)域。 下面的例子說明了這些方法的使用形式。

36、 【例】繪制多邊形示例。 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); points =n

37、ew 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); ,14.2.4 曲線 這里所講的曲線是指自定義曲線,自定義曲線有兩種形式:打開的曲線和封閉的曲線 。 在Graphics類中,繪制自定義曲線的方法有: DrawCurve()方法 DrawClosedCurve()方法 以及應(yīng)用廣泛的繪制貝塞爾曲線的 DrawBezier()方法 Draw

38、Beziers()方法。,1、DrawCurve()方法 這個方法用光滑的曲線把給定的點連接起來,常用形式有: public void DrawCurve(Pen pen, Point points) 其中,Point結(jié)構(gòu)類型的數(shù)組中指明各節(jié)點,默認(rèn)彎曲強度為0.5,注意數(shù)組中至少要有4個元素。 public void DrawCurve(Pen pen, Point points, float tension) 其中tension指定彎曲強度,該值范圍為0.0f 1.0f,超出此范圍會產(chǎn)生異常,當(dāng)彎曲強度為零時,就是直線。,【例】繪制直線與平滑曲線。 private void Form1_P

39、aint(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(350, 200), new Point(400, 150) ;

40、e.Graphics.DrawLines(redPen, curvePoints); e.Graphics.DrawCurve(greenPen, curvePoints);,2、DrawClosedCurve()方法 這個方法也是用平滑的曲線將各節(jié)點連接起來,但會自動把首尾節(jié)點連接起來構(gòu)成封閉曲線。 3、貝塞爾曲線 每段貝塞爾曲線都需要四個點,第一個點是起始點,第四個點是終止點,第二個點和第三個點控制曲線的形狀。使用DrawBezier()方法繪制一段貝塞爾曲線,使用DrawBeziers()方法繪制多段貝塞爾曲線。常用形式有: public void DrawBezier(Pen pen,

41、 Point pt1, Point pt2, Point pt3, Point pt4) 其中pt1、pt2、pt3、pt4分別指定四個點。 public void DrawBezier(Pen pen, Point points) 其中points是Point結(jié)構(gòu)的數(shù)組,第一段貝塞爾曲線從點數(shù)組中的第一個點到第四個點繪制而成。以后每段曲線只需要三個點:兩個控制點和一個結(jié)束點。前一段曲線的結(jié)束點會自動用作后一段曲線的起始點。,【例】繪制貝塞爾曲線。 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventAr

42、gs 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, bezierPoints); ,14.2.5 橢圓,橢圓是一種特殊的封閉曲線,Graphics類專門提供了繪制橢圓的

43、兩種方法: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定義橢圓的邊框的高度。 public void FillEllipse(Pen pen, Rectangle rect) 填充橢圓的內(nèi)部區(qū)

44、域。其中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定義橢圓的邊框的高度。,14.3圖像的顯示與保存,14.3.1 顯示圖像 可以使用 GDI+ 顯示以文件形式存在的圖像。圖像文件可以是BMP、JPEG、GIF、TIFF、PNG等。實現(xiàn)步驟為: 創(chuàng)建一個Bitmap對象,指明要顯示的圖像文件; 創(chuàng)建一個Graphics對象,表示要使用的繪圖平面; 調(diào)

45、用 Graphics 對象的 DrawImage 方法顯示圖像。 創(chuàng)建Bitmap對象 Bitmap類有很多重載的構(gòu)造函數(shù),其中之一是: Public Bitmap(string filename) 可以利用該構(gòu)造函數(shù)創(chuàng)建Bitmap對象,例如: Bitmap bitmap = new Bitmap(“tu1.jpg”);, DrawImage()方法 Graphics類的DrawImage()方法用于在指定位置顯示原始圖像或者縮放后的圖像。該方法的重載形式非常多,其中之一為: public void DrawImage(Image image, int x, int y, int width

46、, int height) 該方法在x, y按指定的大小顯示圖像。利用這個方法可以直接顯示縮放后的圖像。 【例】假設(shè)窗體中有一個Button按鈕button1,可以在單擊按鈕的事件代碼中顯示圖像。 private void button1_Click(object sender, System.EventArgs e) Bitmap bitmap = new Bitmap(d:testtu1.jpg); Graphics g = this.CreateGraphics(); g.DrawImage(bitmap,3,10,200,200); g.DrawImage(bitmap,250,10,

47、50,50); g.DrawImage(bitmap,350,10,bitmap.Width/2, bitmap.Height/2); ,14.3.2 保存圖像 使用畫圖功能在窗體上繪制出圖形或者圖像后,可以以多種格式保存到圖像文件中. 下面的例子說明了具體的用法。 【例】將繪制的圖形或圖像保存到文件中。 創(chuàng)建一個Windows應(yīng)用程序,設(shè)計畫面:, 添加名稱空間引用 using System.Drawing.Drawing2D; 添加【畫圖】按鈕的Click事件代碼 private void button1_Click(object sender, System.EventArgs e) G

48、raphics 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 = new HatchBrush(HatchStyle.Shingle, Color.White, Color.Black); g.FillEllipse(hatchBrush, rect1); Rectangle rect2=ne

49、w Rectangle(this.Width/4+50,0, this.Width/4,this.Height-100);,hatchBrush = new HatchBrush(HatchStyle.WideUpwardDiagonal, Color.White, Color.Red); g.FillRectangle(hatchBrush,rect2); int x=this.Width-50-this.Width/4; Point points =new Point new Point(x,10), new Point(x+50,60), new Point(x+150,10), new

50、 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 TextureBrush(new Bitmap(e:testm23.jpg); g.FillClosedCurve(myBrush,points);, 添加【保存】按鈕的Click事件代碼 private void button

51、2_Click(object sender, System.EventArgs e) /構(gòu)造一個指定區(qū)域的空圖像 Bitmap image=new Bitmap(this.Width,this.Height-100); /根據(jù)指定區(qū)域得到Graphics對象 Graphics g=Graphics.FromImage(image); /設(shè)置圖像的背景色 g.Clear(this.BackColor); /將圖形畫到Graphics對象中 DrawMyImage(g); try /保存畫到Graphics對象中的圖形 image.Save(d:testtu1.jpg,System.Drawing

52、.Imaging.ImageFormat.Jpeg);,g=this.CreateGraphics(); Rectangle rect=new Rectangle(0,0,this.Width,this.Height-100); g.FillRectangle(new SolidBrush(this.BackColor),rect); MessageBox.Show(保存成功!,恭喜); catch(Exception err) MessageBox.Show(err.Message); 添加【顯示】按鈕的Click事件代碼 private void button3_Click(object

53、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); ,14.4 動畫設(shè)計,14.4.1奔跑的豹子 分別設(shè)計豹子奔跑的八種不同形狀,得到八個圖像,保存到文件名為t1.jpg到t8.jpg中。設(shè)計3只豹子從左向右奔跑的動畫,要求能隨時調(diào)整奔跑的速度。 1)創(chuàng)建一個Windows應(yīng)

54、用程序,設(shè)置窗體的背景色為白色;向設(shè)計窗體拖放三個【PicturePox】控件,【SizeMode】屬性均為【StretchImage】,調(diào)整三只豹子的大小使其看起來有立體感;一個TrackBar控件,兩個Label控件,1個Timer控件。如圖:,源程序如下: using System.Drawing.Drawing2D; namespace Donghua public class Form1 : System.Windows.Forms.Form Bitmap bitmap; int num; private System.Windows.Forms.TrackBar trackBar1

55、; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.PictureBox pictureBox2; private System.Windows.Forms.PictureBox pictureBox3; private Sy

56、stem.ComponentModel.IContainer components; public Form1() InitializeComponent();,this.trackBar1.Minimum=10; this.trackBar1.Maximum=100; this.trackBar1.Value=50; this.timer1.Interval=this.trackBar1.Value; bitmap=new Bitmap8; for(int i=1;i=8;i+) bitmapi-1=new Bitmap(d:testt+i.ToString()+.jpg); num=0;

57、this.timer1.Enabled=true; private void trackBar1_Scroll(object sender, System.EventArgs e) this.timer1.Interval=this.trackBar1.Value; ,private void timer1_Tick(object sender, System.EventArgs e) if(num700) this.pictureBox1.Left=-100; this.pictureBox2.Left=this.pictureBox1.Left-20; this.pictureBox3.L

58、eft=this.pictureBox2.Left-20; ,else this.pictureBox1.Left+=10; this.pictureBox2.Left=this.pictureBox1.Left-20; this.pictureBox3.Left=this.pictureBox2.Left-20; 3)運行,觀察效果。,14.4.2 圖像變換, 創(chuàng)建一個Windows應(yīng)用程序,向窗體拖放1個PictureBox控件,黑色背景;1個ListBox控件,5號字;2個Button控件。設(shè)計畫面:, 源程序如下: using System.Drawing.Drawing2D; namespace WinTest public class Form1 : System.Windows.Forms.Form Bitmap myBitmap; public Form1() InitializeComponent(); string i

溫馨提示

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

評論

0/150

提交評論