C#中圖形編程

來源:互聯網
上載者:User

 

 

 

  像Java一樣,C#提供了一整套相當豐富的類庫、方法以及事件以供開發人員使用。C#還引入了GDI+,它是由GDI演變而來的,具有比GDI更強大的功能而且簡化了程式員的編程工作。所以開發人員運用這些,就可以很方便的開發出具有強大圖形映像功能的應用程式了。本文,筆者就通過一些執行個體像讀者介紹一下C#中的圖形編程的基本知識。

 

  簡單一實例:

 

  首先,讓我們從例子開始,以下是一個最簡單的執行個體:

 

using System;

using System.Windows.Forms;

using System.Drawing;

 

public class Hello:Form {

public Hello() {

this.Paint += new PaintEventHandler(f1_paint);

}

 

private void f1_paint(object sender,PaintEventArgs e) {

Graphics g = e.Graphics;

g.DrawString("你好,C#!",new Font("Verdana",20),

new SolidBrush(Color.Tomato),40,40);

g.DrawRectangle(new Pen(Color.Pink,3),20,20,150,100);

 

}

public static void Main() {

Application.Run(new Hello());

}

 

 

  在上面的執行個體中,我們用到了一個方法:DrawString(),它帶有5個參數。同時,我們發現在運用DrawString()方法以前,我們先建立了一個Graphics類型的對象g=e.Graphics,這就說明了在運用任何圖形類的方法以前我們必須先建立該類的一個執行個體化對象。在DrawString()方法後,我們用到了DrawRectangle()方法,其實我們還可以運用其他的方法來畫橢圓或是多邊形等等。第一個執行個體還是相當簡單易懂的,不是嗎?

 

  變換圖形的度量單位:

 

  在圖形編程中,預設的圖形度量單位是象素。不過,你可以通過修改PageUnit屬性來修改圖形的度量單位,可以是英寸或是毫米等。實現方法如下:

 

Graphics g = e.Graphics;

g.PageUnit = GraphicsUnit.Inch 

 

  操作顏色選擇對話方塊:

 

  在實際運用特別是圖形映像編程過程中,我們可能會經常碰到顏色選擇對話方塊(以及下面要提到的字型選擇對話方塊)。使用顏色選擇對話方塊,我們可以讓使用者來選擇系統預定的顏色以及使用者自訂的顏色。在使用顏色選擇對話方塊之前,我們必須先建立一個ColorDialog類型的對象:

 

ColorDialog cd = new ColorDialog(); 

 

  然後,我們就可以用ShowDialog()方法來顯示顏色選擇對話方塊了。之後,就可以通過調用使用者的顏色選擇進行相關的圖形操作了。

 

  以下,我給大家一個執行個體。該執行個體中有一個按鈕和一個文字框,通過點擊按鈕可以調出顏色選擇對話方塊,根據使用者的顏色選擇就可以設定文字框的背景顏色了。

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

public class Clr:Form{

Button b1 = new Button();

TextBox tb = new TextBox();

ColorDialog clg = new ColorDialog();

 

public Clr(){

b1.Click += new EventHandler(b1_click);

b1.Text = "選擇顏色";

tb.Location = new Point(50,50);

this.Controls.Add(b1);

this.Controls.Add(tb);

}

 

public void b1_click(object sender, EventArgs e){

clg.ShowDialog();

tb.BackColor = clg.Color;

}

 

public static void Main() {

Application.Run(new Clr());

}

 

}

 

 

  操作字型選擇對話方塊:

 

  字型是圖形編程的一個重要組成部分,通過設定不同的字型,你可以在程式中達到不同的視覺效果。和以上的顏色選擇對話方塊的建立差不多,你可以很方便地建立一個字型選擇對話方塊,並通過它來讓使用者選擇其所需的字型。

 

  下面同樣給出一個執行個體,這個執行個體和上面的執行個體差不多,只是用來字型選擇對話方塊代替了原來的顏色選擇對話方塊,最後是根據使用者的字型選擇來設定文字框的字型。

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

public class Fonts:Form {

Button b1 = new Button();

TextBox tb = new TextBox();

FontDialog flg = new FontDialog();

 

public Fonts() {

b1.Click += new EventHandler(b1_click);

b1.Text = "選擇字型";

tb.Location = new Point(50,50);

 

this.Controls.Add(b1);

this.Controls.Add(tb);

}

 

 

public void b1_click(object sender, EventArgs e) {

clg.ShowDialog();

tb.FontName = flg.Font;

}

 

 

public static void Main() {

Application.Run(new Fonts());

}

 

 

  使用System.Drawing.Drawing2D名字空間:

 

  如果你有一些圖形映像編程的經驗,那麼你一定知道畫筆和畫刷的概念。它們在圖形編程有非常廣泛的運用。System.Drawing.Drawing2D名字空間提供了相當強大的功能,能讓開發人員很容易地操作畫筆以及畫刷對象。比如,你可以通過設定畫筆的DashStyle屬性(有Dash、DashDot、Solid等風格)來確定直線的風格。同樣,通過運用SolidBrush、HatchBrush、GradientBrush等畫筆你可以很輕易地修改被填充地區的外觀。比如,你可以用SolidBrush將一個矩形地區用許許多多不同粗細的直線來填充。那麼,我們在什麼時候運用畫筆和畫刷呢?就像上面的例子中那樣,通常一個圖形輪廓(運用DrawXXX()方法)是用畫筆對象來實現的,而一個填充地區(運用FillXXX()方法)則是用畫刷對象來實現的。

 

  使用畫筆對象:

 

  在下面的執行個體中,我們用到了System.Drawing.Drawing2D名字空間。執行個體中我們用畫筆以不同的風格畫了直線、橢圓、餡餅圖形、多邊形等圖形。

 

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

 

public class Drawgra:Form {

public Drawgra() {

this.Text = "運用畫筆樣本";

this.Size = new Size(450,400);

this.Paint += new PaintEventHandler(Draw_Graphics);

}

 

public void Draw_Graphics(object sender,PaintEventArgs e) {

Graphics g = e.Graphics;

Pen penline = new Pen(Color.Red,5);

Pen penellipse = new Pen(Color.Blue,5);

Pen penpie = new Pen(Color.Tomato,3);

Pen penpolygon = new Pen(Color.Maroon,4);

 

/*DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等風格*/

//以Dash風格畫一條直線

 

penline.DashStyle = DashStyle.Dash;

g.DrawLine(penline,50,50,100,200);

 

//以DashDotDot風格畫一個橢圓

 

penellipse.DashStyle = DashStyle.DashDotDot;

g.DrawEllipse(penellipse,15,15,50,50);

 

 

//以Dot風格畫一個餡餅圖形

penpie.DashStyle = DashStyle.Dot;

g.DrawPie(penpie,90,80,140,40,120,100);

 

//以Solid風格畫一個多邊形

 

g.DrawPolygon(penpolygon,new Point[]{

new Point(30,140),

new Point(270,250),

new Point(110,240),

new Point(200,170),

new Point(70,350),

new Point(50,200)});

 

}

 

 

public static void Main() {

Application.Run(new Drawgra());

}

 

}  使用畫刷對象:

 

  畫刷對象是用特定的顏色、模式或是映像來填充一塊地區的。總共有四種類型的畫刷:SolidBrush(預設的畫刷)、HatchBrush、GradientBrush以及TexturedBrush。下面,我們分別給出執行個體來進行介紹。

 

  1、運用SolidBrush:

 

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

 

public class Solidbru:Form {

public Solidbru() {

this.Text = "運用SolidBrush樣本";

this.Paint += new PaintEventHandler(Fill_Graph);

}

 

 

public void Fill_Graph(object sender,PaintEventArgs e) {

Graphics g = e.Graphics;

//建立一把SolidBrush並用它來填充一個矩形地區

SolidBrush sb = new SolidBrush(Color.Pink);

g.FillRectangle(sb,50,50,150,150);

 

}

 

 

public static void Main() {

Application.Run(new Solidbru());

}

 

 

 

  2、運用HatchBrush:

 

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

public class Hatchbru:Form {

public Hatchbru() {

this.Text = "運用HatchBrush樣本";

this.Paint += new PaintEventHandler(Fill_Graph);

}

 

public void Fill_Graph(object sender,PaintEventArgs e) {

Graphics g = e.Graphics;

//建立一把HatchBrush並用它來填充一個矩形地區

/*該畫刷的HatchStyle有DiagonalCross、

ForwardDiagonal、Horizontal、 Vertical、 Solid等不同風格 */

HatchStyle hs = HatchStyle.Cross;

HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red);

g.FillRectangle(sb,50,50,150,150);

}

 

 

public static void Main() {

Application.Run(new Hatchbru());

}

 

  3、運用GradientBrush:

 

  GradientBrush又可分為LinearGradientBrush和PathGradientBrush兩種,從它們的名稱我們可以知道前者是線性漸層的,而後者則是路徑漸層的,因而能創造出更複雜和完美的效果。下面我就給大家分別舉例:

 

  1)、運用LinearGradientBrush:

 

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

 

public class LinearGradientbru:Form {

public LinearGradientbru() {

this.Text = "運用LinearGradientBrush樣本";

this.Paint += new PaintEventHandler(Fill_Graph);

}

 

 

public void Fill_Graph(object sender,PaintEventArgs e) {

Rectangle r = new Rectangle(500, 300, 100, 100);

LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow,

LinearGradientMode.BackwardDiagonal);

e.Graphics.FillRectangle(lb, r);

}

 

 

public static void Main() {

Application.Run(new LinearGradientbru());

}

}

 

 

  所得圖形如下:

 

 

 

  2)、運用PathGradientBrush:

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

 

public class PathGradientbru:Form {

public PathGradientbru() {

this.Text = "運用PathGradientBrush樣本";

this.Paint += new PaintEventHandler(Fill_Graph);

}

 

public void Fill_Graph(object sender,PaintEventArgs e) {

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;

e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);

e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);

 

//先設定好一個路徑

GraphicsPath path = new GraphicsPath(new Point[] {

new Point(40, 140),

new Point(275, 200),

new Point(105, 225),

new Point(190, 300),

new Point(50, 350),

new Point(20, 180),

}, new byte[] {

 

(byte)PathPointType.Start,

(byte)PathPointType.Bezier,

(byte)PathPointType.Bezier,

(byte)PathPointType.Bezier,

(byte)PathPointType.Line,

(byte)PathPointType.Line,

});

 

//建立一把PathGradientBrush

 

PathGradientBrush pgb = new PathGradientBrush(path);

 

//設定畫刷的周圍顏色

 

pgb.SurroundColors = new Color[] {

 Color.Green,

 Color.Yellow,

 Color.Red,

 Color.Blue,

 Color.Orange,

 Color.White,

 

};

 

//用畫刷進行填充

e.Graphics.FillPath(pgb, path);

}

 

 

public static void Main() {

Application.Run(new PathGradientbru());

}

 

 

  所得圖形如下:

 

 

  4、運用TexturedBrush:

 

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

 

public class Texturedbru:Form {

Brush bgbrush;

 

public Texturedbru() {

 

//建立一幅映像以供填充橢圓的背景用

Image bgimage = new Bitmap("dotnet.gif");

bgbrush = new TextureBrush(bgimage);

this.Paint+=new PaintEventHandler(Text_bru);

 

}

 

public void Text_bru(object sender,PaintEventArgs e) {

Graphics g = e.Graphics;

g.FillEllipse(bgbrush,50,50,500,300);

 

}

 

public static void Main() {

Application.Run(new Texturedbru());

}

 

  使用映像:

 

  映像在圖形編程中經常要用到的,其對使用者介面的作用也是非常明顯的。在以前的編程過程中,對映像的操作細節是相當繁瑣的而且很容易出錯。現在,在GDI+下面,你可以用C#語言很容易的完成你所希望的映像編程。

 

  很簡單,你只要通過以下步驟就可以實現映像的編程。

 

  1、 建立一個位元影像類對象如下:

 

   Image img = new Bitmap("image.bmp");

 

  2、 在DrawImage()方法中運用上述對象:

 

   g.DrawImage(img,20,20,100,90);

 

  至於使用映像的執行個體,限於篇幅,我就不再這裡介紹了。相信大家能很容易地完成一個運用映像的執行個體的。

 

  總結:

 

  在這篇文章中,我主要用到了兩個非常核心的名字空間:一個是System.Drawing、一個是System.Drawing.Drawing2D。有了它們,我們就可以很方便的調用其中的方法、屬性來實現以往進行圖形編程需要付出很大代價才能完成的任務,這不能不說是GDI+給我們帶來的優點。所以,掌握好GDI+,我相信你的圖形編程能力會更上一層樓的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.