本篇主題內容是.NET GDI+圖形圖像編程系列的教程,不要被這個滾動條嚇到,爲了查找方便,我沒有分開寫,上面加了目錄了,並且不少都是源碼和圖片~html
(*^_^*) 本人也爲了學習深入,另外一方面也是爲了分享給你們,純手工碼了好幾天的字,喜歡的表忘了點贊哦~給點小小的動力~編程
目錄:數組
《GDI+繪圖基礎》
ide1 GDI+概述函數
2 Graphics類工具
2.1 Graphics類的方法成員post
2.2 引用命名空間學習
3 經常使用畫圖對象測試
3.1 Pen類字體
3.2 Color結構
3.3 Font類
3.4 Brush類
3.5 Rectangle類
4 基本圖形繪製舉例
4.1 畫一個矩形
4.2 畫一個弧
4.3 畫線
4.4 畫橢圓
4.5 輸出文本
4.6 填充路徑
5 畫刷和畫刷類型
5.1 SolidBrush(單色畫刷)
5.2 HatchBrush(陰影畫刷)
5.4 LinearGradientBrush 和 PathGradientBrush(漸變筆刷)
5.4.1 LinearGradientBrush類
5.4.2 PathGradientBrush類
GDI+繪圖基礎
編寫圖形程序時須要使用GDI(Graphics Device Interface,圖形設備接口)。
從程序設計的角度看,GDI包括兩部分:GDI對象和GDI函數。GDI對象定義了GDI函數使用的工具和環境變量;而GDI函數使用GDI對象繪製各類圖形。
在C#中,進行圖形程序編寫時用到的是GDI+(Graphics Device Interface Plus,圖形設備接口)版本,GDI+是GDI的進一步擴展,它使咱們編程更加方便。
1 GDI+概述
GDI+是微軟在Windows 2000之後操做系統中提供的新的圖形設備接口,其經過一套部署爲託管代碼的類來實現,這套類被稱爲GDI+的「託管類接口」。
GDI+主要提供了一下三類服務:
咱們要進行圖形編程,就必須先講解Graphics類,同時咱們還必須掌握Pen、Brush和Rectangle這幾種類。
GDI+比GDI優越主要表如今兩個方面:
2 Graphics類
Graphics類封裝一個GDI+繪圖圖面,提供將對象繪製到現實設備的方法,Graphics與特定的設備上下文關聯。
畫圖方法都被包括在Graphics類中國,在畫任何對象(例如:Circle Rectangle)時,咱們首先要建立一個Graphics類實例,這個實例至關於創建了一塊畫布,有了畫布才能夠用各類畫圖方法進行繪圖。
繪圖程序的設計過程通常分爲兩個步驟:1.建立Graphics;2.使用Graphics對象的方法繪圖、顯示文本或處理圖像。
一般咱們使用下述三種方法來建立一個Graphics對象:
方法一:利用控件或窗體的Paint事件中的PaintEventArgs
在窗體或控件的Paint事件中接受對圖形對象的引用,做爲PaintEventArgs(PaintEventArgs指定繪製控件所用的Graphics)的一部分,在爲控件建立繪製代碼時,一般會使用此方法來獲取對圖形對象的引用。例如:
1 // 窗體的Paint事件的響應方法 2 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 3 { 4 Graphics _Graphics = e.Graphics; 5 } 6 7 // 也能夠直接重載控件或窗體的OnPaint方法 8 protected override void OnPaint(PaintEventArgs e) 9 { 10 Graphics _Graphics = e.Graphics; 11 }
Paint事件在重繪控件時發生。
方法二:調用某控件或窗體的CreateGraphics方法
調用某控件或窗體的CreateGraphics方法以獲取對Graphics對象的引用,該對象表示該控件或窗體的繪圖圖面。
若是想在已存在的窗體或控件上繪圖,一般會使用此方法,例如:
1 Graphics _Graphics = this.CreateGraphics(); // 在當前窗體上建立Graphics對象
方法三:調用Graphics類的FromImage靜態方法
由從Image集成的任何對象建立Graphics對象。在須要更改已存在的圖像時,一般會使用此方法。例如:
1 Image img = Image.FromFile("孤影.jpg"); // 創建Image對象 2 Graphics _Graphics = Graphics.FromImage(img); // 建立Graphics對象
2.1 Graphics類的方法成員
有了一個Graphics的對象引用後,就能夠利用該對象的成員進行各類各樣圖形的繪製,下面表格列出了Graphics類的經常使用方法成員:
名稱 | 說明 | 名稱 | 說明 |
DrawArc | 畫弧 | DrawBezier | 畫立體的貝爾塞曲線 |
DrawBeziers | 畫連續立體的貝爾塞曲線 | DrawClosedCurve | 畫閉合曲線 |
DrawCurve | 畫曲線 | DrawEllipse | 畫橢圓 |
DrawImage | 畫圖像 | DrawLine | 畫線 |
DrawPath | 經過路徑畫線和曲線 | DrawPie | 畫餅形 |
DrawPolygon | 畫多邊形 | DrawRectangle | 畫矩形 |
DrawString | 繪製文字 | FillEllipse | 填充橢圓 |
FillPath | 填充路徑 | FillPie | 填充餅圖 |
FillPolygon | 填充多邊形 | FillRectangle | 填充矩形 |
FillRectangles | 填充矩形組 | FillRegion | 填充區域 |
在.NET中,GDI+的全部繪圖功能都包括在System、System.Drawimg、System.Drawimg.Imaging、System.Drawimg.Drawimg2D和System.Drawimg.Text等命名空間中,所以開始用GDI+類以前,須要先引用相應的命名空間。
2.2 引用命名空間
在C#應用程序中使用using命令引用給定的命名空間或類,下面是一個C#應用程序引用命名空間的例子:
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.ComponentModel; 5 using System.Drawing; 6 using System.Drawing.Drawing2D; 7 using System.Drawing.Imaging;
3 經常使用畫圖對象
在建立了Graphics對象後,就能夠用它開始繪圖了,能夠畫線、填充圖形、顯示文本等等,其中主要用到的對象還有:
3.1 Pen類
Pen類用來繪製指定寬度和樣式的直線。使用DashStyle屬性繪製幾種虛線,可使用各類各樣填充樣式(包括純色和紋理)來填充Pen繪製的直線,填充模式取決於畫筆或用做填充對象的紋理。
使用畫筆時,須要先實例化一個畫筆對象,主要有如下幾種方法:
1 // 用指定的顏色實例化一隻畫筆 2 public Pen(Color); 3 4 // 用指定的畫刷實例化一隻畫筆 5 public Pen(Brush); 6 7 // 用指定的畫刷和寬度實例化一隻畫筆 8 public Pen(Brush, float); 9 10 // 用指定的顏色和寬度實例化一隻畫筆 11 public Pen(Color, float); 12 13 // 實例化畫筆格式以下: 14 Pen pen = new Pen(Color.Blue); 15 // 或者: 16 Pen pen = new Pen(Color.Blue, 100);
Pen經常使用的屬性以下:
名稱 | 說明 | 名稱 | 說明 |
Alignment | 得到或者設置畫筆的對齊方式 | Brush | 得到或者設置畫筆的屬性 |
Color | 得到或者設置畫筆的顏色 | Width | 得到或者設置畫筆的寬度 |
3.2 Color結構
在天然界中,顏色大都由透明度(A)和三基色(R,G,B)所組成。在GDI+中,經過Color結構封裝對顏色的定義,Color結構中,除了提供(A,R,G,B)之外,還提供許多系統定義的顏色,如Pink(粉色)。另外,還提供許多靜態成員,用戶對顏色進行操做。
Color結構的基本屬性以下表:
名稱 | 說明 |
A | 獲取此Color結構的Alpha份量值,取值(0~255) |
R | 獲取此Color結構的紅色份量值,取值(0~255) |
G | 獲取此Color結構的綠色份量值,取值(0~255) |
B | 獲取此Color結構的藍色份量值,取值(0~255) |
Name | 獲取此Color結構的名稱,這將返回用戶定義的顏色的名稱或已知顏色的名稱(若是該顏色是從某個名稱建立的)。 對於自定義的顏色,這將返回RGB值。 |
Color結構的基本(靜態)方法以下表:
名稱 | 說明 |
FromArgb | 從四個8位的ARGB份量(Alpha、紅色、綠色和藍色)值建立Color結構 |
FromKnowColor | 從指定餓預約義顏色建立一個Color結構 |
FromName | 從預約義顏色的指定名稱建立一個Color結構。 |
Color結構變量能夠經過已有顏色構造,也能夠經過RGB創建,例如:
1 Color color1 = Color.FromArgb(96, 06, 25); 2 Color color2 = Color.FromKnownColor(KnownColor.Blue); // KnowColor爲枚舉類型 3 Color color3 = Color.FromName("LightBlue");
在圖像處理中通常須要獲取或設置像素的顏色值,獲取一幅圖像的某個像素顏色值得具體步驟以下:
1 Color color = new Color(); 2 color = bitmap.GetPixel(10, 10); // 獲取此Bitmap中指定像素的顏色
1 int r, g, b; 2 r = color.R; 3 g = color.G; 4 b = color.B;
3.3 Font類
Font類定義特定文本格式,包括字體、字號和字形屬性。Font類的經常使用構造函數是:
public Font(string 字體名, float 字號, FontStyle 字形){} 其中字號和字體爲可選項
public Font(string 字體名, float 字號) 其中字體名爲Font的FontFamily的字符串表示形式
下面是定義一個F哦你團隊相愛難過的示例代碼:
1 FontFamily fontFamily = new FontFamily("Arial"); 2 Font font = new Font(fontFamily, 16, FontStyle.Regular, GraphicsUnit.Pixel);
字體經常使用屬性以下表:
名稱 | 說明 | 名稱 | 說明 |
Bold | 是否爲粗體 | FontFamily | 字體成員 |
Height | 字體高 | Italic | 是否爲斜體 |
Name | 字體名稱 | Size | 字體尺寸 |
SizeInPoints | 獲取此Font對象的字號,以磅爲單位 | Strikeout | 是否有刪除線 |
Style | 字體類型 | Underline | Unit |
Unit | 字體尺寸單位 |
3.4 Brush類
Brush類是一個抽象的基類,所以它不能被實例化,咱們老是用它的派生類進行實例化一個畫刷的對象,當咱們對圖形內部進行填充操做時就會用到畫刷,關於畫刷在 [1.5] 中有詳細的講解。
3.5 Rectangle結構
存儲一組整數,共四個,表示一個矩形的位置和大小。
矩形結構一般用來在窗體上畫矩形,除了利用它的構造函數矩形對象外,還能夠利用Rectangle結構的屬性成員,其屬性成員以下表:
名稱 | 說明 | 名稱 | 索命 |
Bottom | 底端座標 | Height | 矩形高 |
IsEmpty | 測試矩形寬和高是否爲0 | Left | 矩形左邊座標 |
Location | 矩形的位置 | Right | 矩形右邊座標 |
Size | 矩形尺寸 | Top | 矩形頂端座標 |
Width | 矩形寬 | X | 矩形左上角頂點X座標 |
Y | 矩形左上角頂點Y座標 |
Rectangle結構的構造函數有如下兩個:
1 // 用指定的位置和大小初始化Rectangle類的新實例 2 public Rectangle(Point, Size); // Size結構存儲一個有序整數對,一般爲矩形的寬度和高度 3 public Rectangle(int, int, int, int);
1.3.6 Point結構
用指定座標初始化Point類的新實例,這個結構很像C++的Point結構,它描述了一對有序的x,y兩個座標值,其構造函數爲:
public Point(int x, int y); 其中x爲該點的水平位置;y爲該點的垂直位置。
下面是構造Point對象的示例代碼:
1 Point pt1 = new Point(30, 30); 2 Point pt2 = new Point(110, 110);
4 基本圖形繪製舉例
4.1 畫一個矩形
建一個C#.NET WinForms窗體應用程序,經過在窗體的OnPaint事件中繪製一個填充的漸變矩形:
填充矩形方法FillRectangle()的語法幫助定義以下:
1 // 2 // 摘要: 3 // 填充 System.Drawing.Rectangle 結構指定的矩形的內部。 4 // 5 // 參數: 6 // brush: 7 // 肯定填充特性的 System.Drawing.Brush。 8 // 9 // rect: 10 // System.Drawing.Rectangle 結構,它表示要填充的矩形。 11 // 12 // 異常: 13 // System.ArgumentNullException: 14 // brush 爲 null。 15 public void FillRectangle(Brush brush, Rectangle rect); 16 // 17 // 摘要: 18 // 填充 System.Drawing.RectangleF 結構指定的矩形的內部。 19 // 20 // 參數: 21 // brush: 22 // 肯定填充特性的 System.Drawing.Brush。 23 // 24 // rect: 25 // System.Drawing.RectangleF 結構,它表示要填充的矩形。 26 // 27 // 異常: 28 // System.ArgumentNullException: 29 // brush 爲 null。 30 public void FillRectangle(Brush brush, RectangleF rect); 31 // 32 // 摘要: 33 // 填充由一對座標、一個寬度和一個高度指定的矩形的內部。 34 // 35 // 參數: 36 // brush: 37 // 肯定填充特性的 System.Drawing.Brush。 38 // 39 // x: 40 // 要填充的矩形的左上角的 x 座標。 41 // 42 // y: 43 // 要填充的矩形的左上角的 y 座標。 44 // 45 // width: 46 // 要填充的矩形的寬度。 47 // 48 // height: 49 // 要填充的矩形的高度。 50 // 51 // 異常: 52 // System.ArgumentNullException: 53 // brush 爲 null。 54 public void FillRectangle(Brush brush, float x, float y, float width, float height); 55 // 56 // 摘要: 57 // 填充由一對座標、一個寬度和一個高度指定的矩形的內部。 58 // 59 // 參數: 60 // brush: 61 // 肯定填充特性的 System.Drawing.Brush。 62 // 63 // x: 64 // 要填充的矩形的左上角的 x 座標。 65 // 66 // y: 67 // 要填充的矩形的左上角的 y 座標。 68 // 69 // width: 70 // 要填充的矩形的寬度。 71 // 72 // height: 73 // 要填充的矩形的高度。 74 // 75 // 異常: 76 // System.ArgumentNullException: 77 // brush 爲 null。 78 public void FillRectangle(Brush brush, int x, int y, int width, int height);
咱們在這裏只使用第一種定義,演示填充矩形,示例代碼以下:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics g = e.Graphics; // 建立當前窗體的Graphics對象 9 Rectangle rect = new Rectangle(50, 30, 100, 100); // 建立一個矩形(x,y,width,height) 10 // 建立線性漸變畫刷(畫刷界限, 起始顏色, 結束顏色, 漸變角度) 11 LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Purple, Color.LightBlue, LinearGradientMode.BackwardDiagonal); 12 g.FillRectangle(lBrush, rect); // 走起~ 13 }
上述代碼運行效果以下:
4.2 畫一個弧
畫弧線的語法定義以下:
1 // 2 // 摘要: 3 // 繪製一段弧線,它表示 System.Drawing.Rectangle 結構指定的橢圓的一部分。 4 // 5 // 參數: 6 // pen: 7 // System.Drawing.Pen,它肯定弧線的顏色、寬度和樣式。 8 // 9 // rect: 10 // System.Drawing.RectangleF 結構,它定義橢圓的邊界。 11 // 12 // startAngle: 13 // 從 x 軸到弧線的起始點沿順時針方向度量的角(以度爲單位)。 14 // 15 // sweepAngle: 16 // 從 startAngle 參數到弧線的結束點沿順時針方向度量的角(以度爲單位)。 17 // 18 // 異常: 19 // System.ArgumentNullException: 20 // pen 爲 null。 21 public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepAngle);
參照定義的幫助,可寫出以下畫弧線的代碼:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics graphics = e.Graphics; 9 Pen pen = new Pen(Color.Blue); 10 Rectangle rect = new Rectangle(50,50,200,100); 11 graphics.DrawArc(pen, rect, 12, 84); 12 }
上述代碼運行結果以下:
4.3 畫線
畫線DrawLine()方法的語法定義以下:
1 // 2 // 摘要: 3 // 繪製一條鏈接兩個 System.Drawing.Point 結構的線。 4 // 5 // 參數: 6 // pen: 7 // System.Drawing.Pen,它肯定線條的顏色、寬度和樣式。 8 // 9 // pt1: 10 // System.Drawing.Point 結構,它表示要鏈接的第一個點。 11 // 12 // pt2: 13 // System.Drawing.Point 結構,它表示要鏈接的第二個點。 14 // 15 // 異常: 16 // System.ArgumentNullException: 17 // pen 爲 null。 18 public void DrawLine(Pen pen, Point pt1, Point pt2); 19 // 20 // 摘要: 21 // 繪製一條鏈接兩個 System.Drawing.PointF 結構的線。 22 // 23 // 參數: 24 // pen: 25 // System.Drawing.Pen,它肯定線條的顏色、寬度和樣式。 26 // 27 // pt1: 28 // System.Drawing.PointF 結構,它表示要鏈接的第一個點。 29 // 30 // pt2: 31 // System.Drawing.PointF 結構,它表示要鏈接的第二個點。 32 // 33 // 異常: 34 // System.ArgumentNullException: 35 // pen 爲 null。 36 public void DrawLine(Pen pen, PointF pt1, PointF pt2); 37 // 38 // 摘要: 39 // 繪製一條鏈接由座標對指定的兩個點的線條。 40 // 41 // 參數: 42 // pen: 43 // System.Drawing.Pen,它肯定線條的顏色、寬度和樣式。 44 // 45 // x1: 46 // 第一個點的 x 座標。 47 // 48 // y1: 49 // 第一個點的 y 座標。 50 // 51 // x2: 52 // 第二個點的 x 座標。 53 // 54 // y2: 55 // 第二個點的 y 座標。 56 // 57 // 異常: 58 // System.ArgumentNullException: 59 // pen 爲 null。 60 public void DrawLine(Pen pen, float x1, float y1, float x2, float y2); 61 // 62 // 摘要: 63 // 繪製一條鏈接由座標對指定的兩個點的線條。 64 // 65 // 參數: 66 // pen: 67 // System.Drawing.Pen,它肯定線條的顏色、寬度和樣式。 68 // 69 // x1: 70 // 第一個點的 x 座標。 71 // 72 // y1: 73 // 第一個點的 y 座標。 74 // 75 // x2: 76 // 第二個點的 x 座標。 77 // 78 // y2: 79 // 第二個點的 y 座標。 80 // 81 // 異常: 82 // System.ArgumentNullException: 83 // pen 爲 null。 84 public void DrawLine(Pen pen, int x1, int y1, int x2, int y2);
根據定義的幫助,咱們以第一種語法 能夠寫出以下示例代碼:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics graphics = e.Graphics; // 建立當前窗體的Graphics對象 9 Pen pen = new Pen(Color.Blue); // 建立藍色畫筆對象 10 Point pointStart = new Point(30, 30); // 建立起始點 11 Point pointEnd = new Point(150, 150); // 建立結束點 12 graphics.DrawLine(pen, pointStart, pointEnd); // 畫線 13 }
上述代碼運行效果圖以下:
4.4 畫橢圓
仍是先看一下DrawEllipse()畫橢圓的語法定義:
1 // 2 // 摘要: 3 // 繪製邊界 System.Drawing.Rectangle 結構指定的橢圓。 4 // 5 // 參數: 6 // pen: 7 // System.Drawing.Pen,它肯定曲線的顏色、寬度和樣式。 8 // 9 // rect: 10 // System.Drawing.Rectangle 結構,它定義橢圓的邊界。 11 // 12 // 異常: 13 // System.ArgumentNullException: 14 // pen 爲 null。 15 public void DrawEllipse(Pen pen, Rectangle rect); 16 // 17 // 摘要: 18 // 繪製邊界 System.Drawing.RectangleF 定義的橢圓。 19 // 20 // 參數: 21 // pen: 22 // System.Drawing.Pen,它肯定曲線的顏色、寬度和樣式。 23 // 24 // rect: 25 // System.Drawing.RectangleF 結構,它定義橢圓的邊界。 26 // 27 // 異常: 28 // System.ArgumentNullException: 29 // pen 爲 null。 30 public void DrawEllipse(Pen pen, RectangleF rect); 31 // 32 // 摘要: 33 // 繪製一個由邊框(該邊框由一對座標、高度和寬度指定)定義的橢圓。 34 // 35 // 參數: 36 // pen: 37 // System.Drawing.Pen,它肯定曲線的顏色、寬度和樣式。 38 // 39 // x: 40 // 定義橢圓的邊框的左上角的 X 座標。 41 // 42 // y: 43 // 定義橢圓的邊框的左上角的 Y 座標。 44 // 45 // width: 46 // 定義橢圓的邊框的寬度。 47 // 48 // height: 49 // 定義橢圓的邊框的高度。 50 // 51 // 異常: 52 // System.ArgumentNullException: 53 // pen 爲 null。 54 public void DrawEllipse(Pen pen, float x, float y, float width, float height); 55 // 56 // 摘要: 57 // 繪製一個由邊框定義的橢圓,該邊框由矩形的左上角座標、高度和寬度指定。 58 // 59 // 參數: 60 // pen: 61 // System.Drawing.Pen,它肯定曲線的顏色、寬度和樣式。 62 // 63 // x: 64 // 定義橢圓的邊框的左上角的 X 座標。 65 // 66 // y: 67 // 定義橢圓的邊框的左上角的 Y 座標。 68 // 69 // width: 70 // 定義橢圓的邊框的寬度。 71 // 72 // height: 73 // 定義橢圓的邊框的高度。 74 // 75 // 異常: 76 // System.ArgumentNullException: 77 // pen 爲 null。 78 public void DrawEllipse(Pen pen, int x, int y, int width, int height);
參照上面的語法定義,咱們根據第一種語法,能夠寫出以下示例代碼:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics graphics = e.Graphics; // 建立當前窗體的Graphics對象 9 Pen pen = new Pen(Color.Blue, 100); // 建立藍色 粗細爲100的畫筆對象 10 Rectangle rect = new Rectangle(50, 50, 200, 100); // 建立橢圓所在的矩形範圍 11 graphics.DrawEllipse(pen, rect); // 在指定的範圍內畫橢圓 12 }
上述代碼運行結果以下:
4.5 輸出文本
輸出文本用到的是Graphics對象的DrawString()方法,語法定義以下:
1 // 2 // 摘要: 3 // 在指定位置而且用指定的 System.Drawing.Brush 和 System.Drawing.Font 對象繪製指定的文本字符串。 4 // 5 // 參數: 6 // s: 7 // 要繪製的字符串。 8 // 9 // font: 10 // System.Drawing.Font,它定義字符串的文本格式。 11 // 12 // brush: 13 // System.Drawing.Brush,它肯定所繪製文本的顏色和紋理。 14 // 15 // point: 16 // System.Drawing.PointF 結構,它指定所繪製文本的左上角。 17 // 18 // 異常: 19 // System.ArgumentNullException: 20 // brush 爲 null。 - 或 - s 爲 null。 21 public void DrawString(string s, Font font, Brush brush, PointF point); 22 // 23 // 摘要: 24 // 在指定矩形而且用指定的 System.Drawing.Brush 和 System.Drawing.Font 對象繪製指定的文本字符串。 25 // 26 // 參數: 27 // s: 28 // 要繪製的字符串。 29 // 30 // font: 31 // System.Drawing.Font,它定義字符串的文本格式。 32 // 33 // brush: 34 // System.Drawing.Brush,它肯定所繪製文本的顏色和紋理。 35 // 36 // layoutRectangle: 37 // System.Drawing.RectangleF 結構,它指定所繪製文本的位置。 38 // 39 // 異常: 40 // System.ArgumentNullException: 41 // brush 爲 null。 - 或 - s 爲 null。 42 public void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle); 43 // 44 // 摘要: 45 // 在指定位置而且用指定的 System.Drawing.Brush 和 System.Drawing.Font 對象繪製指定的文本字符串。 46 // 47 // 參數: 48 // s: 49 // 要繪製的字符串。 50 // 51 // font: 52 // System.Drawing.Font,它定義字符串的文本格式。 53 // 54 // brush: 55 // System.Drawing.Brush,它肯定所繪製文本的顏色和紋理。 56 // 57 // x: 58 // 所繪製文本的左上角的 x 座標。 59 // 60 // y: 61 // 所繪製文本的左上角的 y 座標。 62 // 63 // 異常: 64 // System.ArgumentNullException: 65 // brush 爲 null。 - 或 - s 爲 null。 66 public void DrawString(string s, Font font, Brush brush, float x, float y); 67 // 68 // 摘要: 69 // 使用指定 System.Drawing.StringFormat 的格式化特性,用指定的 System.Drawing.Brush 和 System.Drawing.Font 70 // 對象在指定的位置繪製指定的文本字符串。 71 // 72 // 參數: 73 // s: 74 // 要繪製的字符串。 75 // 76 // font: 77 // System.Drawing.Font,它定義字符串的文本格式。 78 // 79 // brush: 80 // System.Drawing.Brush,它肯定所繪製文本的顏色和紋理。 81 // 82 // point: 83 // System.Drawing.PointF 結構,它指定所繪製文本的左上角。 84 // 85 // format: 86 // System.Drawing.StringFormat,它指定應用於所繪製文本的格式化特性(如行距和對齊方式)。 87 // 88 // 異常: 89 // System.ArgumentNullException: 90 // brush 爲 null。 - 或 - s 爲 null。 91 public void DrawString(string s, Font font, Brush brush, PointF point, StringFormat format); 92 // 93 // 摘要: 94 // 使用指定 System.Drawing.StringFormat 的格式化特性,用指定的 System.Drawing.Brush 和 System.Drawing.Font 95 // 對象在指定的矩形繪製指定的文本字符串。 96 // 97 // 參數: 98 // s: 99 // 要繪製的字符串。 100 // 101 // font: 102 // System.Drawing.Font,它定義字符串的文本格式。 103 // 104 // brush: 105 // System.Drawing.Brush,它肯定所繪製文本的顏色和紋理。 106 // 107 // layoutRectangle: 108 // System.Drawing.RectangleF 結構,它指定所繪製文本的位置。 109 // 110 // format: 111 // System.Drawing.StringFormat,它指定應用於所繪製文本的格式化特性(如行距和對齊方式)。 112 // 113 // 異常: 114 // System.ArgumentNullException: 115 // brush 爲 null。 - 或 - s 爲 null。 116 public void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format); 117 // 118 // 摘要: 119 // 使用指定 System.Drawing.StringFormat 的格式化特性,用指定的 System.Drawing.Brush 和 System.Drawing.Font 120 // 對象在指定的位置繪製指定的文本字符串。 121 // 122 // 參數: 123 // s: 124 // 要繪製的字符串。 125 // 126 // font: 127 // System.Drawing.Font,它定義字符串的文本格式。 128 // 129 // brush: 130 // System.Drawing.Brush,它肯定所繪製文本的顏色和紋理。 131 // 132 // x: 133 // 所繪製文本的左上角的 x 座標。 134 // 135 // y: 136 // 所繪製文本的左上角的 y 座標。 137 // 138 // format: 139 // System.Drawing.StringFormat,它指定應用於所繪製文本的格式化特性(如行距和對齊方式)。 140 // 141 // 異常: 142 // System.ArgumentNullException: 143 // brush 爲 null。 - 或 - s 爲 null。 144 public void DrawString(string s, Font font, Brush brush, float x, float y, StringFormat format);
根據上述定義,咱們以 public void DrawString(string s, Font font, Brush brush, float x, float y); 語法能夠寫出以下示例代碼:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Font font = new Font("華文行楷", 40); // 建立Font字體對象 9 Graphics graphics = e.Graphics; // 建立當前窗體的Graphics對象 10 graphics.DrawString("孤影'Blog 歡迎您!", font, new SolidBrush(Color.Black), 30, 60); 11 }
上述代碼運行結果以下:
4.6 填充路徑
填充路徑的語法定義以下:
1 // 2 // 摘要: 3 // 填充 System.Drawing.Drawing2D.GraphicsPath 的內部。 4 // 5 // 參數: 6 // brush: 7 // 肯定填充特性的 System.Drawing.Brush。 8 // 9 // path: 10 // System.Drawing.Drawing2D.GraphicsPath,它表示要填充的路徑。 11 // 12 // 異常: 13 // System.ArgumentNullException: 14 // brush 爲 null。 - 或 - path 爲 null。 15 public void FillPath(Brush brush, GraphicsPath path);
根據上述語法定義,可寫出以下示例代碼:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics graphics = e.Graphics; // 建立當前窗體的Graphics對象 9 graphics.FillRectangle(new SolidBrush(Color.White), this.ClientRectangle); // 以白色畫刷填充當前窗體 10 // 建立線組 11 GraphicsPath path = new GraphicsPath(new Point[] { 12 new Point(40,140), 13 new Point(275,200), 14 new Point(105,225), 15 new Point(190,300), 16 new Point(50,350), 17 new Point(20,180) 18 }, new byte[] { 19 (byte)PathPointType.Start, 20 (byte)PathPointType.Bezier, 21 (byte)PathPointType.Bezier, 22 (byte)PathPointType.Bezier, 23 (byte)PathPointType.Line, 24 (byte)PathPointType.Line 25 }); 26 // 路徑筆刷 27 PathGradientBrush pathGradientBrush = new PathGradientBrush(path); 28 // 設置路徑中的點對應的顏色數組 29 pathGradientBrush.SurroundColors = new Color[] { Color.Green, Color.Yellow, Color.Red, Color.Blue, Color.Orange, Color.White }; 30 graphics.FillPath(pathGradientBrush, path); // 填充路徑 31 }
上述代碼運行結果以下:
注意:GraphicsPath類位於命名空間「System.Drawimg.Drawimg2D」中,表示一系列相互鏈接的直線和曲線。
5 畫刷和畫刷類型
Brush類型是一個抽象類,因此它不能被實例化,也就是不能直接應用,可是咱們能夠利用他的派生類,如:HatchBrush、SolidBrush和TextureBrush等。
畫刷類型通常在「System.Drawing」命名空間中,若是應用HatchBrush和GradientBrush畫刷,須要在程序中引入「System.Drawing2D」命名空間。
5.1 SolidBrush(單色畫刷)
它是一種通常的畫刷,一般只用一種顏色去填充GDI+圖形,例如以下示例代碼:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics graphics = e.Graphics; // 建立當前窗體的Graphics對象 9 SolidBrush solidBrushR = new SolidBrush(Color.Red); // 紅色畫刷 10 SolidBrush solidBrushG = new SolidBrush(Color.Green); // 綠色畫刷 11 SolidBrush solidBrushB = new SolidBrush(Color.Blue); // 藍色畫刷 12 13 graphics.FillEllipse(solidBrushG, 20, 40, 60, 70); // 用綠色畫刷填充一個橢圓 14 15 Rectangle rect = new Rectangle(0, 0, 200, 100); // 矩形 16 graphics.FillPie(solidBrushB, 0, 0, 200, 40, 0.0f, 30.0f); // 填充餅圖 17 18 // 組成多邊形的點 19 PointF point1 = new PointF(50.0f, 250.0f); 20 PointF point2 = new PointF(100.0f, 25.0f); 21 PointF point3 = new PointF(150.0f, 40.0f); 22 PointF point4 = new PointF(200.0f, 50.0f); 23 PointF point5 = new PointF(250.0f, 100.0f); 24 PointF[] curvePoints = { point1, point2, point3, point4, point5 }; 25 graphics.FillPolygon(solidBrushR, curvePoints); // 填充多邊形 26 }
運行效果以下:
5.2 HatchBrush(陰影畫刷)
HatchBrush類位於「System.Drawing.Drawing2D」命名空間中。陰影畫刷有兩種顏色:前景色和背景色,以及6種陰影。前景色定義線條的顏色,背景色定義線條之間間隙的顏色。
HatchBrush類有兩個構造函數:
HatchStyle枚舉值指定可用於HatchBrush對象的不一樣圖案,主要成員以下:
名稱 | 說明 | 名稱 | 說明 |
BackwardDiagonal | 從右上到左下的對角線的線條圖案 | Cross | 指定交叉的水平線和垂直線 |
DarkDownwardDiagonal | 指定從頂點到底點向右傾斜的對角線,其兩邊夾角比ForwardDiagonal小50%,寬度是其兩倍。此陰影圖案不是鋸齒消除的 | DarkHorizontal | 指定水平線的兩邊夾角比Horizontal小50%,而且寬度是Horizontal的兩倍 |
DarkUpwardDiagonal | 指定從頂點到底點向左傾斜的對角線,其兩邊夾角比BackwardDiagonal小50%,寬度是其兩倍,但這些直線不是鋸齒消除的 | DarkVertical | 指定垂直線的兩邊夾角比Vertical小50%,而且寬度是其兩倍 |
DashedDownwardDiagonal | 指定虛線對角線,這些對角線從頂點到底點向右傾斜 | DashedHorizontal | 指定虛線水平線 |
DashedUpwardDiagonal | 指定虛線對角線,這些對角線從頂點到底點向左傾斜 | DashedVertical | 指定虛線垂直線 |
DiagonalBrick | 指定具備分層磚塊外觀的陰影,它從頂點到底點向左傾斜 | DiagonalCross | 交叉對角線的圖案 |
Divot | 指定具備草皮層外觀的陰影 | ForwardDiagonal | 從坐上到右下分層磚塊外觀的陰影 |
Horizontal | 水平線的圖案 | HorizontalBrick | 指定具備水平分層磚塊外觀的陰影 |
LargeGrid | 指定陰影樣式Cross | LightHorizontal | 指定水平線,其兩邊夾角比Horizontal小50% |
LightVertical | 指定垂直線的兩邊夾角比Vertical小50% | Max | 指定陰影樣式SolidDiamond |
Min | 指定陰影樣式Horizontal | NarrowHorizontal | 指定水平線的兩邊夾角比陰影樣式Horizontal小75%(或者比LightHorizontal小25%) |
NarrowVertical | 指定垂直線的兩邊夾角比陰影樣式Vertical小75%(或者比LightVertical小25%) | OutlineDiamond | 指定互相交叉的正向對角線和反向對角線,但這些對角線不是鋸齒消除的 |
Percent05 | 指定5%陰影,前景色與背景色的比例爲5:100 | Percent90 | 指定90%陰影,前景色與背景色的比例爲90:100 |
Plaid | 指定具備格子花呢材料外觀的陰影 | Shingle | 指定帶有對角分層鵝卵石外觀的陰影,它從頂點到底點向右傾斜 |
SmallCheckerBoard | 指定帶有期盼外觀的陰影 | SmallDiamond | 指定具備對角放置的棋盤外觀的陰影 |
Sphere | 指定具備球體彼此相鄰放置的外觀的陰影 | Trellis | 指定具備格架外觀的陰影 |
Vertical | 垂直線的圖案 | Wave | 指定由代字號"~"構成的水平線 |
Weave | 指定具備織物外觀的陰影 |
咱們隨便挑選三個樣式,以下示例代碼:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics graphics = e.Graphics; // 建立當前窗體的Graphics對象 9 // 建立用於畫三種不一樣樣式圖形的陰影畫筆 10 HatchBrush hatchBrushR = new HatchBrush(HatchStyle.DiagonalCross, Color.Chocolate, Color.Red); 11 HatchBrush hatchBrushG = new HatchBrush(HatchStyle.DashedHorizontal, Color.Green, Color.Black); 12 HatchBrush hatchBrushB = new HatchBrush(HatchStyle.Weave, Color.BlueViolet, Color.Blue); 13 14 graphics.FillEllipse(hatchBrushR, 20, 80, 60, 20); // 填充橢圓 15 16 // 填充餅圖 17 Rectangle rect = new Rectangle(0, 0, 200, 100); 18 graphics.FillPie(hatchBrushB, 0, 0, 200, 40, 0.0f, 30.0f); 19 20 // 填充自定義圖形 21 PointF point1 = new PointF(50.0f, 250.0f); 22 PointF point2 = new PointF(100.0f, 25.0f); 23 PointF point3 = new PointF(150.0f, 40.0f); 24 PointF point4 = new PointF(250.0f, 50.0f); 25 PointF point5 = new PointF(300.0f, 100.0f); 26 PointF[] curvePoints = { point1, point2, point3, point4, point5 }; 27 graphics.FillPolygon(hatchBrushG, curvePoints); 28 }
上述代碼運行結果以下:
5.3 TextureBrush(紋理畫刷)
紋理畫刷擁有圖案,而且一般使用它來填充封閉的圖形。爲了對它初始化,可使用一個已經存在的別人設計好了的圖案,或使用經常使用的設計程序設計的本身的圖案,同時應該使圖案存儲爲經常使用圖形文件格式,如BMP格式文件,這有一個設計好的位圖:「LonelyShadow.bmp」文件,紋理畫刷使用的示例代碼以下:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics graphics = e.Graphics; // 建立當前窗體的Graphics對象 9 Bitmap bitmap = new Bitmap("LonelyShadow.bmp"); // 根據文件建立原始大小的Bitmap對象 10 bitmap = new Bitmap(bitmap, this.ClientRectangle.Size); // 縮放到窗體大小 11 TextureBrush textureBrush = new TextureBrush(bitmap); 12 graphics.FillEllipse(textureBrush, this.ClientRectangle); 13 }
上述代碼運行效果以下:
5.4 LinearGradientBrush 和 PathGradientBrush(漸變畫刷)
漸變畫刷相似於實心畫刷,由於他也是基於顏色的,與實心畫刷不一樣的是:漸變畫刷使用兩種顏色,它的主要特色是:在使用過程當中,一種顏色在一段,而另一種顏色在另外一端,在中間,兩種顏色融合產生過分或衰減的效果。
漸變畫刷有兩種:線性畫刷和路徑畫刷(LinearGradientBrush 和 PathGradientBrush)。
其中LinearGradientBrush能夠顯示線性漸變效果,而PathGradientBrush是路徑漸變的能夠顯示比較具備彈性的漸變效果。
5.4.1 LinearGradientBrush類
LinearGradientBrush類構造函數以下:
1 public LinerGradientBrush(Point point1, Point point2, Color color1, Color color2) 2 3 // point1: 表示線性漸變的起始點的Point結構 4 5 // point2: 表示線性漸變的終結點的Point結構 6 7 // color1: 表示線性漸變的起始顏色的Color結構 8 9 // color2: 表示線性漸變的結束顏色的Color結構
咱們能夠寫出以下示例代碼:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics graphics = e.Graphics; // 建立當前窗體的Graphics對象 9 LinearGradientBrush linearGradientBrush = new LinearGradientBrush(this.ClientRectangle, Color.White, Color.Blue, LinearGradientMode.Vertical); 10 graphics.FillRectangle(linearGradientBrush, this.ClientRectangle); 11 }
上述代碼運行效果以下:
5.4.2 PathGradientBrush類
PathGradientBrush類的構造函數以下:public PathGradientBrush(GraphicsPath path); // path: GraphicsPath,定義此PathGradientBrush填充區域
PathGradientBrush使用的示例代碼以下:
1 /// <summary> 2 /// 窗體的Paint事件的響應方法 3 /// </summary> 4 /// <param name="sender">當前事件觸發者(當前窗體)</param> 5 /// <param name="e">附帶的事件參數</param> 6 private void Frm_Demo_Paint(object sender, PaintEventArgs e) 7 { 8 Graphics graphics = e.Graphics; // 建立當前窗體的Graphics對象 9 Point centerPoint = new Point(150, 100); 10 int R = 60; 11 GraphicsPath path = new GraphicsPath(); 12 path.AddEllipse(centerPoint.X - R, centerPoint.Y - R, R * 2, R * 2); 13 PathGradientBrush brush = new PathGradientBrush(path); 14 brush.CenterPoint = centerPoint; // 指定路徑中心點 15 brush.CenterColor = Color.Red; // 指定路徑中心的顏色 16 brush.SurroundColors = new Color[] { Color.Plum }; 17 graphics.FillEllipse(brush, centerPoint.X - R, centerPoint.Y - R, R * 2, R * 2); 18 centerPoint = new Point(350, 100); 19 R = 20; 20 path = new GraphicsPath(); 21 path.AddEllipse(centerPoint.X - R, centerPoint.Y - R, R * 2, R * 2); 22 path.AddEllipse(centerPoint.X - R * 2, centerPoint.Y - R * 2, R * 4, R * 4); 23 path.AddEllipse(centerPoint.X - R * 3, centerPoint.Y - R * 3, R * 6, R * 6); 24 brush = new PathGradientBrush(path); 25 brush.CenterPoint = centerPoint; 26 brush.CenterColor = Color.Red; 27 brush.SurroundColors = new Color[] { Color.Black, Color.Blue, Color.Green }; 28 graphics.FillPath(brush, path); 29 }
上述代碼運行效果圖以下:
哈哈,看到這裏,本篇的內容也就講完了,文章看似很長,其實內容主題也就是GDI+繪圖編程了,只是示例代碼多了一點點。
這段時間碼字真的很累的。以爲還不錯的,請留言或點個贊支持我一下~
親們也能夠點擊關注我,博客大部分都是原創,後期我還會發GDI+示例文章,常常來個人博客哦~
有什麼問題或建議也能夠留言哦~謝謝親們的支持。mua~