GDI+圖形圖像處理技術中Pen和Brush的簡單使用和簡單圖形的繪制(C#)


1.Graphics

  Graphics對象是GDI+繪圖表面,因此在Windows窗體應用程序中要使用GDI+創建繪圖,必須要先創建Graphics.在給窗體注冊一個Paint事件后,Graphics的創建方式有以下三種。

1)直接獲取Paint事件的PaintEvenlArgs中Graphics對象(最常用)。

1 private void Form1_Paint(object sender, PaintEventArgs e)
2 {
3     Graphics g1 = e.Graphics;
4 }    

2)從Image中創建Graphics對象。

1         private void Form1_Paint(object sender, PaintEventArgs e)
2         {
3             //創建一個Image對象
4             Bitmap imgTemp = new Bitmap(200, 200);
5             Graphics g2 = Graphics.FromImage(imgTemp);
6         }    

3)用CreateGraphics方法創建Graphics對象。

1         private void Form1_Paint(object sender, PaintEventArgs e)
2         {
3             Graphics g3 = this.CreateGraphics();
4         }

2.Pen

  在Graphics這張圖紙准備好了以后,我們就可以使用Pen(鋼筆)進行繪圖了。Pen類位於System.Drawing名稱空間中(在Windows窗體應用程序當中不需要單獨引用)。最簡單的創建方式為:

Pen p = new Pen(Color.White,1);        //創建顏色為白色,像素為1的鋼筆

  下面為Pen的一個應用實例

1 private void Form1_Paint(object sender, PaintEventArgs e)
2 {
3     Graphics g1 = e.Graphics;
4 
5     Pen p = new Pen(Color.Blue, 2);     //定義筆顏色藍色,大小2個像素
6     g1.DrawEllipse(p, 0, 80, 60, 30);   //繪制一個橢圓,橢圓的外切矩形左上坐標為(0,80),矩形長(x)寬(y)為(60,30)
7     p.Dispose();        //釋放Pen所占用的資源
8     g1.Dispose();       //釋放由Graphics使用的資源
9 }

  繪圖結果如下

Pen應用示例

3.Brush

  Brush(筆刷)和Pen類似,不過Brush類本身是一個抽象類不能直接實例化。GDI+的API提供五個類,擴展並提供了Brush類的具體實現。這5個類分別是:

1)SolidBrush  使用純顏色填充圖形 

應用實例

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4     
 5     //SolidBrush的使用: 使用純顏色填充圖形
 6     Brush bh = new SolidBrush(Color.Black);
 7     g.FillEllipse(bh, 0, 80, 60, 30);       //繪制一個橢圓
 8     bh.Dispose();   //釋放資源
 9     g.Dispose();
10 }

繪圖結果:

2)TextureBrush  使用基於光柵的圖像(位圖,JPG等圖像)填充圖形

應用實例:

  首先找一種要填充的圖片,最好在項目中創建一個img文件夾,將其添加到文件夾中

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4 
 5     //TextureBrush的使用:使用基於光柵的圖像(位圖,JPG等圖像)填充圖形
 6     string path = @"E:\軟件\Microsoft Visual Studio 10.0\project\CSharp\22GDI+圖形圖像處理技術\22_3創建Brush對象\22_3創建Brush對象\imgs\1.png";
 7     Bitmap img;
 8     if (File.Exists(path))
 9     {
10         img = new Bitmap(path);
11         Brush br = new TextureBrush(img);
12         g.FillEllipse(br, 0, 80, 650, 280);
13         br.Dispose();
14     }
15     else
16         MessageBox.Show("要填充的圖片不存在!");
17     g.Dispose();
18 }

繪圖結果

3)LinearGradientBrush  使用顏色漸變填充圖形

應用實例

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4     
 5     //LinearGradientBrush:使用顏色線性漸變填充圖形(需要單獨引用System.Drawing.Drawing2D)
 6     //設置漸變的起點坐標(0,110),終點坐標(60,110),顏色分別為白色和黑色
 7     LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 110), new Point(60, 110), Color.White, Color.FromArgb(255, 0, 0, 0));
 8     g.FillEllipse(lgb, 0, 80, 60, 30);
 9     lgb.Dispose();
10     g.Dispose();
11 }

繪圖結果

4)PathGradientBrush  使用漸變色填充圖形,漸變方向是從有路徑定義的圖形邊界指向圖形中心

應用實例

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4 
 5     //PathGradientBrush:以自定義路徑的方式漸變填充圖形(需要單獨引用System.Drawing.Drawing2D)
 6     //給命名空間改名未避免錯誤,應該再命名空間上右鍵->重構->重命名(F2)
 7     GraphicsPath gp = new GraphicsPath();       //創建路徑
 8     gp.AddEllipse(0, 80, 60, 30);               //添加路徑
 9     PathGradientBrush pgb = new PathGradientBrush(gp);      //根據路徑創建筆刷
10     pgb.CenterColor = Color.FromArgb(255, 255, 0, 0);     //設定中間顏色
11     Color[] colors = { Color.FromArgb(255, 0, 255, 0) };
12     pgb.SurroundColors = colors;                //設定環繞顏色
13     e.Graphics.FillEllipse(pgb, 0, 80, 60, 30);     //使用筆刷繪圖
14     pgb.Dispose();
15     g.Dispose();
16 }

繪圖結果

5)HatchBrush  使用各類圖案填充圖形

 應用實例

 1  private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;
 4 
 5     //HatchBrush:使用各類圖案填充圖形(需要單獨引用System.Drawing.Drawing2D)
 6     HatchBrush hb = new HatchBrush(HatchStyle.HorizontalBrick, Color.Red, Color.Yellow);    //第一個參數枚舉值可以按需求更改
 7     g.FillEllipse(hb, 0, 80, 60, 30);
 8     hb.Dispose();
 9     g.Dispose();
10 }

繪圖結果

4.綜合示例:繪制各種形狀

 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2 {
 3     Graphics g = e.Graphics;        //獲取Graphice畫板
 4     Pen p = new Pen(Color.Red, 3);  //建立Pen對象
 5     g.DrawRectangle(p, 10, 10, 40,60);  //繪制左上角坐標(10,10),長寬為(40,60)的矩形
 6 
 7     p.Color = Color.Green;          //更改畫筆顏色為綠色
 8     Point[] triangleP = {new Point(50,10),
 9                       new Point(60,70),
10                       new Point(80,40)};
11     g.DrawPolygon(p, triangleP);        //根據給定坐標繪制多邊形(三角形)
12 
13     Point[] pentacleP = {new Point(0,76),
14                          new Point(80,76),
15                          new Point(106,0),
16                          new Point(130,76),
17                          new Point(210,76),
18                          new Point(146,124),
19                          new Point(170,200),
20                          new Point(106,152),
21                          new Point(40,200),
22                          new Point(66,124)
23                         };
24     g.DrawPolygon(p, pentacleP);        //繪制正五邊形
25 
26     p.Color = Color.Cyan;           //改變畫筆顏色
27     g.DrawArc(p, 150, 0, 50, 50, 0, 90);    //繪制圓弧,
28     //(150,0)決定橢圓外切圓左上坐標.(50,50)決定矩形的長寬.(0,90)決定起始角度和掃過的角度
29 
30     g.DrawPie(p, 150, 90, 50, 50, 0, 90);   //繪制扇形,參數列表和繪制圓弧一樣
31 
32 
33     p.Dispose();    //釋放Pen資源
34     g.Dispose();    //釋放Graphics資源
35 }

繪圖結果

Ps:學習過程中應用的小技巧。

1.給命名空間改名未避免錯誤,應該再命名空間上右鍵->重構->重命名(F2)

2.需要打特殊符號時,如版權符號。可以用win+R調出命令窗口,輸入charmap回車,然后再里面選擇。

3.使用DrawPolygon方法繪制多邊形時,可以先使用其他繪圖工具(如CAD)得到多邊形的頂點坐標。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM